package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.truck;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.wxInform.WxInformBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.CIcCardsInfoMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.AddTrucksQueueVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.CTrucksUpdateVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.TrucksVo;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import lombok.SneakyThrows;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 车辆排队业务层
 *
 * @author xiaoxu
 * @version 1.0
 * @date 2023/1/13 15:00
 * @since 1.0
 */
@Component
public class TruckQueueBu {
    @Autowired
    private ICTrucksService iCTrucksService;
    @Autowired
    private ICWeightSheetsService icWeightSheetsService;
    @Autowired
    private ICAshGateVideoBindService icAshGateVideoBindService;
    @Autowired
    private ICCompanysService iCCompanysService;
    @Autowired
    private ICProductsInfoService iCProductsInfoService;
    @Autowired
    private ICIcCardsInfoService iCIcCardsInfoService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ICPdtsAuthService iCPdtsAuthService;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private ICTrucksUpdateService iCTrucksUpdateService;
    @Autowired
    private CIcCardsInfoMapper cIcCardsInfoMapper;
    @Autowired
    private QueueHandlingBu queueHandlingBu;
    @Autowired
    private ISysDicService sysDicService;
    @Autowired
    private TruckQueueZxBu truckQueueZxBu;
    @Autowired
    private WxInformBu wxInformBu;
    @Autowired
    private ICPdtClassService iCPdtClassService;
    @Autowired
    private ITGypsumPlanService iTGypsumPlanService;
    @Autowired
    private ICAshGateVideoBindService iCAshGateVideoBindService;
@Autowired
private ITAshGateCollocationService iTAshGateCollocationService;

    //    startTime,endTime,pdtId,truckNo,coId
    @SneakyThrows
    public SzResult getAllWaitTrucks(String token, String startTime, String endTime, List<String> pdtId, String truckNo, String coId, String icCardNo,String iccardAlias, String page, String pageSize, String plantId) {
        //String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        Page<CTrucks> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("TRUCKS_QUEUE_STATUS", 0);
        queryWrapper.in(pdtId != null && pdtId.size()>0, "TRUCKS_CHECKIN_PDT", pdtId);
        queryWrapper.eq(coId != null && !coId.equals(""), "TRUCKS_CHECKIN_CO", coId);
        queryWrapper.eq(icCardNo != null && !icCardNo.equals(""), "TRUCKS_CHECKIN_CARDNO", icCardNo);
        if (iccardAlias !=null && !iccardAlias.equals("")) {
            QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("CARDINF_DELETED", 0);
            queryWrapper3.eq("DATA_FROM", plantId);
            queryWrapper3.like("CARDINF_ALIAS", iccardAlias);
            List<CIcCardsInfo> cIcCardsInfos = iCIcCardsInfoService.list(queryWrapper3);
            if (cIcCardsInfos.size()>0) {
                List<String> list = new ArrayList<>();
                for (CIcCardsInfo cIcCardsInfo : cIcCardsInfos) {
                    list.add(cIcCardsInfo.getCardinfCardno());
                }
                queryWrapper.in(  "TRUCKS_CHECKIN_CARDNO", list);
            }
        }
        queryWrapper.ne("TRUCKS_DELETED", 1);
        queryWrapper.like(truckNo != null && !truckNo.equals(""), "TRUCKS_TRUCKNO", truckNo);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.between(startTime != null && !startTime.equals("") && endTime != null && !endTime.equals(""), "TRUCKS_CHECKIN_TM", startTime, endTime);
        queryWrapper.orderByDesc("TRUCKS_QUEUE_STATUS");
        queryWrapper.orderByAsc("TRUCKS_CHECKIN_PDT", "TRUCKS_CHECKIN_LOADSITE", "TRUCKS_CHECKIN_NO");
//按照TRUCKS_CHECKIN_PDT分组
        //queryWrapper.groupBy("TRUCKS_CHECKIN_PDT");
//        新建一个list
        IPage<CTrucks> iPage = iCTrucksService.page(page1, queryWrapper);
        List<CTrucks> list = iPage.getRecords();
        long total = iPage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        List<TrucksVo> trucksVos = new ArrayList<>();
        for (CTrucks cTrucks : list) {
            TrucksVo trucksVo = new TrucksVo();
            trucksVo.setTrucksCheckinLoadsiteSn(cTrucks.getTrucksCheckinLoadsiteSn());
            trucksVo.setPriceId(cTrucks.getPriceId());
            trucksVo.setPriceType(cTrucks.getPriceType());
            trucksVo.setSupplyCoId(cTrucks.getSupplyCoId());
            if (cTrucks.getSupplyCoId() != null) {
                CCompanys cCompanys = iCCompanysService.getById(cTrucks.getSupplyCoId());
                if (cCompanys != null) {
                    trucksVo.setSupplyCoName(cCompanys.getCpyCo());
                }
            }
            trucksVo.setSpecialPrice(cTrucks.getSpecialPrice());
            trucksVo.setTrucksId(String.valueOf(cTrucks.getTrucksId()));
            trucksVo.setTrucksSn(cTrucks.getTrucksSn());
            trucksVo.setTrucksTruckno(cTrucks.getTrucksTruckno());
            trucksVo.setTrucksTrailerno(cTrucks.getTrucksTrailerno());
            trucksVo.setTrucksType(cTrucks.getTrucksType());
            trucksVo.setTrucksPriv(cTrucks.getTrucksPriv());
            trucksVo.setTrucksEmiss(cTrucks.getTrucksEmiss());
            trucksVo.setTrucksAxles(cTrucks.getTrucksAxles());
            trucksVo.setTrucksLength(cTrucks.getTrucksLength());
            trucksVo.setTrucksMaster(cTrucks.getTrucksMaster());
            trucksVo.setTrucksIdentity(cTrucks.getTrucksIdentity());
            trucksVo.setTrucksTell(cTrucks.getTrucksTell());
            trucksVo.setTrucksTag1(cTrucks.getTrucksTag1());
            trucksVo.setTrucksTag2(cTrucks.getTrucksTag2());
            trucksVo.setTrucksTag3(cTrucks.getTrucksTag3());
            trucksVo.setTrucksRefTare(cTrucks.getTrucksRefTare());
            trucksVo.setTrucksRefNet(cTrucks.getTrucksRefNet());
            trucksVo.setTrucksAgainMinTm(cTrucks.getTrucksAgainMinTm());
            trucksVo.setTrucksDispatchPdt(cTrucks.getTrucksDispatchPdt());
            trucksVo.setTrucksDispatchCo(cTrucks.getTrucksDispatchCo());
            trucksVo.setTrucksDispatchTm(cTrucks.getTrucksDispatchTm());
            trucksVo.setTrucksDispatcher(cTrucks.getTrucksDispatcher());
            trucksVo.setTrucksRemark(cTrucks.getTrucksRemark());
            trucksVo.setTrucksCheckinNo(cTrucks.getTrucksCheckinNo());
            trucksVo.setTrucksCheckinCardno(cTrucks.getTrucksCheckinCardno());
            QueryWrapper<CIcCardsInfo> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.eq("CARDINF_CARDNO", cTrucks.getTrucksCheckinCardno());
            queryWrapper4.eq("CARDINF_DELETED", 0);
            CIcCardsInfo cIcCardsInfo = iCIcCardsInfoService.getOne(queryWrapper4);
            if (cIcCardsInfo != null) {
                trucksVo.setTrucksCheckinCardnoAlias(cIcCardsInfo.getCardinfAlias());
            }
            trucksVo.setTrucksCheckinCo(cTrucks.getTrucksCheckinCo());
            if (cTrucks.getTrucksCheckinCo() == null || cTrucks.getTrucksCheckinCo().equals("")) {
                trucksVo.setTrucksCheckinCoName("");
                trucksVo.setTrucksCheckinChiCoName("");
                //
            } else {
                QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("ID", cTrucks.getTrucksCheckinCo());
                queryWrapper1.eq("CPY_DELETED", 0);
                CCompanys cCompanys = iCCompanysService.getOne(queryWrapper1);
                if (cCompanys == null) {
                    trucksVo.setTrucksCheckinCoName("");
                    trucksVo.setTrucksCheckinChiCoName("");
                } else {
                    trucksVo.setTrucksCheckinCoName(cCompanys.getCpyCo());
                    trucksVo.setTrucksCheckinChiCoName(cCompanys.getCpyChico());
                }
            }
            trucksVo.setTrucksCheckinTag(cTrucks.getTrucksCheckinTag());
            trucksVo.setTrucksCheckinPdt(cTrucks.getTrucksCheckinPdt());
            if (cTrucks.getTrucksCheckinPdt() == null || cTrucks.getTrucksCheckinPdt().equals("")) {
                trucksVo.setTrucksCheckinPdtName("");
            } else {
                QueryWrapper<CProductsInfo> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("ID", cTrucks.getTrucksCheckinPdt());
                queryWrapper2.eq("PDTINF_DELETED", 0);
                CProductsInfo cProductsInfo = iCProductsInfoService.getOne(queryWrapper2);
                if (cProductsInfo == null)
                    trucksVo.setTrucksCheckinPdtName("");
                else
                    trucksVo.setTrucksCheckinPdtName(cProductsInfo.getPdtinfPdt());
            }
            trucksVo.setTrucksCheckinPdtsn(cTrucks.getTrucksCheckinPdtsn());
            trucksVo.setTrucksCheckinCoTran(cTrucks.getTrucksCheckinCoTran());

            if (cTrucks.getTrucksCheckinCoTran() == null || cTrucks.getTrucksCheckinCoTran().equals("")) {
                trucksVo.setTrucksCheckinCoTranName("");

            }
            else {
                QueryWrapper<CCompanys> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("ID", cTrucks.getTrucksCheckinCoTran());
                queryWrapper3.eq("CPY_DELETED", 0);
                CCompanys cCompanys1 = iCCompanysService.getOne(queryWrapper3);
                if (cCompanys1 == null) {
                    trucksVo.setTrucksCheckinCoTranName("");
                } else {
                    trucksVo.setTrucksCheckinCoTranName(cCompanys1.getCpyCo());
                }
            }
            if (StringUtils.isNotEmpty(cTrucks.getBindCoId())) {
                List<Map<String, Object>> coList = new ArrayList<>();
                for (String id : Arrays.asList(cTrucks.getBindCoId().split(","))) {
                    QueryWrapper<CCompanys> queryWrapper5 = new QueryWrapper<>();
                    queryWrapper5.eq("ID", id);
                    queryWrapper5.eq("CPY_DELETED", 0);
                    queryWrapper5.eq("DATA_FROM", plantId);

                    for (CCompanys cCompanys : iCCompanysService.list(queryWrapper5)) {
                        Map<String, Object> resMap = new HashMap<>();
                        resMap.put("coName", cCompanys.getCpyCo() + cCompanys.getCpyChico());
                        resMap.put("id", cCompanys.getId());
                        coList.add(resMap);
                    }
                }
                trucksVo.setCoList(coList);
            }
            trucksVo.setTrucksCheckinBatchSn(cTrucks.getTrucksCheckinBatchSn());
            trucksVo.setTrucksCheckinBatch(cTrucks.getTrucksCheckinBatch());
            trucksVo.setTrucksCheckinLoadsite(cTrucks.getTrucksCheckinLoadsite());
//            trucksPreDeduct
            trucksVo.setTrucksPreDeduct(cTrucks.getTrucksPreDeduct());
//            trucksBillGross
            trucksVo.setTrucksBillGross(cTrucks.getTrucksBillGross());
//trucksBillTare
            trucksVo.setTrucksBillTare(cTrucks.getTrucksBillTare());
            //trucksBillNet
            trucksVo.setTrucksBillNet(cTrucks.getTrucksBillNet());
            //trucksDepStation
            trucksVo.setTrucksDepStation(cTrucks.getTrucksDepStation());
            //trucksBoat
            trucksVo.setTrucksBoat(cTrucks.getTrucksBoat());
            //trucksShip
            trucksVo.setTrucksShip(cTrucks.getTrucksShip());
            //trucksMineral
            trucksVo.setTrucksMineral(cTrucks.getTrucksMineral());
            //trucksCoaltype
            trucksVo.setTrucksCoaltype(cTrucks.getTrucksCoaltype());
//            trucksShipTm
            trucksVo.setTrucksShipTm(cTrucks.getTrucksShipTm());
            //trucksDelyTm
            trucksVo.setTrucksDelyTm(cTrucks.getTrucksDelyTm());
            //trucksCheckinTm
            trucksVo.setTrucksCheckinTm(cTrucks.getTrucksCheckinTm());
//            trucksCallinTm

            trucksVo.setTrucksCallinTm(cTrucks.getTrucksCallinTm());
//            trucksEnterTm
            trucksVo.setTrucksEnterTm(cTrucks.getTrucksEnterTm());
            //trucksPoundinTm
            trucksVo.setTrucksPoundinTm(cTrucks.getTrucksPoundinTm());
            //trucksPoundoutTm
            trucksVo.setTrucksPoundoutTm(cTrucks.getTrucksPoundoutTm());
            //trucksCheckinOperator
            trucksVo.setTrucksCheckinOperator(cTrucks.getTrucksCheckinOperator());
            //trucksRegMan
            trucksVo.setTrucksRegMan(cTrucks.getTrucksRegMan());
            //trucksFixMan
            trucksVo.setTrucksFixMan(cTrucks.getTrucksFixMan());
            //trucksFixTm
            trucksVo.setTrucksFixTm(cTrucks.getTrucksFixTm());
            //trucksQueuePreStatus
            trucksVo.setTrucksQueuePreStatus(cTrucks.getTrucksQueuePreStatus());
            //trucksQueueStatus
            trucksVo.setTrucksQueueStatus(cTrucks.getTrucksQueueStatus());
            //trucksSampling
            trucksVo.setTrucksSampling(cTrucks.getTrucksSampling());
            //gpsDeviceNo
            trucksVo.setGpsDeviceNo(cTrucks.getTrucksGpsDevNo());
            //svrTmstamp
            trucksVo.setSvrTmstamp(cTrucks.getSvrTmstamp());
            //cliTmstamp
            trucksVo.setCliTmstamp(cTrucks.getCliTmstamp());
            //trucksCheckinIdentied
            trucksVo.setTrucksCheckinIdentied(cTrucks.getTrucksCheckinIdentied());
            //trucksEntered
            trucksVo.setTrucksEntered(cTrucks.getTrucksEntered());
            //trucksBreakdown
            trucksVo.setTrucksBreakdown(cTrucks.getTrucksBreakdown());
            //trucksLinger
            trucksVo.setTrucksLinger(cTrucks.getTrucksLinger());
            //trucksRefTareInited
            trucksVo.setTrucksRefTareInited(cTrucks.getTrucksRefTareInited());
            //trucksMustQueue
            trucksVo.setTrucksMustQueue(cTrucks.getTrucksMustQueue());
            //trucksIdentyQueue
            trucksVo.setTrucksIdentyQueue(cTrucks.getTrucksIdentyQueue());
            //trucksIdentyLoad
            trucksVo.setTrucksIdentyLoad(cTrucks.getTrucksIdentyLoad());
            //trucksInfoApproved
            trucksVo.setTrucksInfoApproved(cTrucks.getTrucksInfoApproved());
            //trucksDeleted
            trucksVo.setTrucksDeleted(cTrucks.getTrucksDeleted());
            //trucksNew
            trucksVo.setTrucksNew(cTrucks.getTrucksNew());
//            downedID1
            trucksVo.setDownedID1(cTrucks.getDownedID1());
//            downedID2
            trucksVo.setDownedID2(cTrucks.getDownedID2());
//            downedID3
            trucksVo.setDownedID3(cTrucks.getDownedID3());
//            downedID4
            trucksVo.setDownedID4(cTrucks.getDownedID4());
//            downedID5
            trucksVo.setDownedID5(cTrucks.getDownedID5());
//            downedID6
            trucksVo.setDownedID6(cTrucks.getDownedID6());
//            downedID7
            trucksVo.setDownedID7(cTrucks.getDownedID7());
//            downedID8
            trucksVo.setDownedID8(cTrucks.getDownedID8());
//            downedID9
            trucksVo.setDownedID9(cTrucks.getDownedID9());
//            downedID10
            trucksVo.setDownedID10(cTrucks.getDownedID10());
//            downedID11
            trucksVo.setDownedID11(cTrucks.getDownedID11());
//            downedID12
            trucksVo.setDownedID12(cTrucks.getDownedID12());
//            downedID13
            trucksVo.setDownedID13(cTrucks.getDownedID13());
//            downedID14
            trucksVo.setDownedID14(cTrucks.getDownedID14());
//            downedID15
            trucksVo.setDownedID15(cTrucks.getDownedID15());
//            downedID16
            trucksVo.setDownedID16(cTrucks.getDownedID16());
//            downedID17
            trucksVo.setDownedID17(cTrucks.getDownedID17());
//            downedID18
            trucksVo.setDownedID18(cTrucks.getDownedID18());
//            downedID19
            trucksVo.setDownedID19(cTrucks.getDownedID19());
//            downedID20
            trucksVo.setDownedID20(cTrucks.getDownedID20());
//            dataFrom
            trucksVo.setDataFrom(cTrucks.getDataFrom());
//            serialNo
            trucksVo.setSerialNo(cTrucks.getSerialNo());

            //            如果车辆是等待状态就不显示装载点等信息
            if (cTrucks.getTrucksQueueStatus().equals(BigDecimal.ONE)) {
                trucksVo.setTrucksCallinTm(null);
                trucksVo.setTrucksCheckinLoadsite(null);
                trucksVo.setSpecialPrice(null);
            }
            trucksVos.add(trucksVo);
        }
        map.put("list", trucksVos);
        return SzResult.success(map);
    }

    @SneakyThrows
    public SzResult getAllBindGpsTrucks(String token, String startTime, String endTime, String pdtId, String truckNo, String coId, String icCardNo, String page, String pageSize, String plantId) {
        //String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        Page<CTrucks> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
//        queryWrapper.ne("TRUCKS_QUEUE_STATUS", 0);
        queryWrapper.eq(pdtId != null && !pdtId.equals(""), "TRUCKS_CHECKIN_PDT", pdtId);
        queryWrapper.eq(coId != null && !coId.equals(""), "TRUCKS_CHECKIN_CO", coId);
        queryWrapper.eq(icCardNo != null && !icCardNo.equals(""), "TRUCKS_CHECKIN_CARDNO", icCardNo);
        queryWrapper.ne("TRUCKS_DELETED", 1);
        queryWrapper.like(truckNo != null && !truckNo.equals(""), "TRUCKS_TRUCKNO", truckNo);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.between(startTime != null && !startTime.equals("") && endTime != null && !endTime.equals(""), "TRUCKS_CHECKIN_TM", startTime, endTime);
        queryWrapper.orderByDesc("TRUCKS_QUEUE_STATUS");
        queryWrapper.orderByAsc("TRUCKS_CHECKIN_PDT", "TRUCKS_CHECKIN_LOADSITE", "TRUCKS_CHECKIN_NO");
//按照TRUCKS_CHECKIN_PDT分组
        //queryWrapper.groupBy("TRUCKS_CHECKIN_PDT");
//        新建一个list
        IPage<CTrucks> iPage = iCTrucksService.page(page1, queryWrapper);
        List<CTrucks> list = iPage.getRecords();
        long total = iPage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        List<TrucksVo> trucksVos = new ArrayList<>();
        for (CTrucks cTrucks : list) {
            TrucksVo trucksVo = new TrucksVo();
            trucksVo.setTrucksCheckinLoadsiteSn(cTrucks.getTrucksCheckinLoadsiteSn());
            trucksVo.setPriceId(cTrucks.getPriceId());
            trucksVo.setPriceType(cTrucks.getPriceType());
            trucksVo.setSupplyCoId(cTrucks.getSupplyCoId());
            if (cTrucks.getSupplyCoId() != null) {
                CCompanys cCompanys = iCCompanysService.getById(cTrucks.getSupplyCoId());
                if (cCompanys != null) {
                    trucksVo.setSupplyCoName(cCompanys.getCpyCo() + "." + cCompanys.getCpyChico());
                }
            }
            trucksVo.setSpecialPrice(cTrucks.getSpecialPrice());
            trucksVo.setTrucksId(String.valueOf(cTrucks.getTrucksId()));
            trucksVo.setTrucksSn(cTrucks.getTrucksSn());
            trucksVo.setTrucksTruckno(cTrucks.getTrucksTruckno());
            trucksVo.setTrucksTrailerno(cTrucks.getTrucksTrailerno());
            trucksVo.setTrucksType(cTrucks.getTrucksType());
            trucksVo.setTrucksPriv(cTrucks.getTrucksPriv());
            trucksVo.setTrucksEmiss(cTrucks.getTrucksEmiss());
            trucksVo.setTrucksAxles(cTrucks.getTrucksAxles());
            trucksVo.setTrucksLength(cTrucks.getTrucksLength());
            trucksVo.setTrucksMaster(cTrucks.getTrucksMaster());
            trucksVo.setTrucksIdentity(cTrucks.getTrucksIdentity());
            trucksVo.setTrucksTell(cTrucks.getTrucksTell());
            trucksVo.setTrucksTag1(cTrucks.getTrucksTag1());
            trucksVo.setTrucksTag2(cTrucks.getTrucksTag2());
            trucksVo.setTrucksTag3(cTrucks.getTrucksTag3());
            trucksVo.setTrucksRefTare(cTrucks.getTrucksRefTare());
            trucksVo.setTrucksRefNet(cTrucks.getTrucksRefNet());
            trucksVo.setTrucksAgainMinTm(cTrucks.getTrucksAgainMinTm());
            trucksVo.setTrucksDispatchPdt(cTrucks.getTrucksDispatchPdt());
            trucksVo.setTrucksDispatchCo(cTrucks.getTrucksDispatchCo());
            trucksVo.setTrucksDispatchTm(cTrucks.getTrucksDispatchTm());
            trucksVo.setTrucksDispatcher(cTrucks.getTrucksDispatcher());
            trucksVo.setTrucksRemark(cTrucks.getTrucksRemark());
            trucksVo.setTrucksCheckinNo(cTrucks.getTrucksCheckinNo());
            trucksVo.setTrucksCheckinCardno(cTrucks.getTrucksCheckinCardno());
            trucksVo.setTrucksCheckinCo(cTrucks.getTrucksCheckinCo());
            if (cTrucks.getTrucksCheckinCo() == null || cTrucks.getTrucksCheckinCo().equals("")) {
                trucksVo.setTrucksCheckinCoName("");
                trucksVo.setTrucksCheckinChiCoName("");
                //
            } else {
                QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("ID", cTrucks.getTrucksCheckinCo());
                queryWrapper1.eq("CPY_DELETED", 0);
                CCompanys cCompanys = iCCompanysService.getOne(queryWrapper1);
                if (cCompanys == null) {
                    trucksVo.setTrucksCheckinCoName("");
                    trucksVo.setTrucksCheckinChiCoName("");
                } else {
                    trucksVo.setTrucksCheckinCoName(cCompanys.getCpyCo());
                    trucksVo.setTrucksCheckinChiCoName(cCompanys.getCpyChico());
                }
            }
            trucksVo.setTrucksCheckinTag(cTrucks.getTrucksCheckinTag());
            trucksVo.setTrucksCheckinPdt(cTrucks.getTrucksCheckinPdt());
            if (cTrucks.getTrucksCheckinPdt() == null || cTrucks.getTrucksCheckinPdt().equals("")) {
                trucksVo.setTrucksCheckinPdtName("");
            } else {
                QueryWrapper<CProductsInfo> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("ID", cTrucks.getTrucksCheckinPdt());
                queryWrapper2.eq("PDTINF_DELETED", 0);
                CProductsInfo cProductsInfo = iCProductsInfoService.getOne(queryWrapper2);
                if (cProductsInfo == null)
                    trucksVo.setTrucksCheckinPdtName("");
                else
                    trucksVo.setTrucksCheckinPdtName(cProductsInfo.getPdtinfPdt());
            }
            trucksVo.setTrucksCheckinPdtsn(cTrucks.getTrucksCheckinPdtsn());
            trucksVo.setTrucksCheckinCoTran(cTrucks.getTrucksCheckinCoTran());

            if (cTrucks.getTrucksCheckinCoTran() == null || cTrucks.getTrucksCheckinCoTran().equals("")) {
                trucksVo.setTrucksCheckinCoTranName("");

            }
            else {
                QueryWrapper<CCompanys> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("ID", cTrucks.getTrucksCheckinCoTran());
                queryWrapper3.eq("CPY_DELETED", 0);
                CCompanys cCompanys1 = iCCompanysService.getOne(queryWrapper3);
                if (cCompanys1 == null) {
                    trucksVo.setTrucksCheckinCoTranName("");
                } else {
                    trucksVo.setTrucksCheckinCoTranName(cCompanys1.getCpyCo());
                }
            }
            if (StringUtils.isNotEmpty(cTrucks.getBindCoId())) {
                List<Map<String, Object>> coList = new ArrayList<>();
                for (String id : Arrays.asList(cTrucks.getBindCoId().split(","))) {
                    QueryWrapper<CCompanys> queryWrapper4 = new QueryWrapper<>();
                    queryWrapper4.eq("ID", id);
                    queryWrapper4.eq("CPY_DELETED", 0);
                    queryWrapper4.eq("DATA_FROM", plantId);

                    for (CCompanys cCompanys : iCCompanysService.list(queryWrapper4)) {
                        Map<String, Object> resMap = new HashMap<>();
                        resMap.put("coName", cCompanys.getCpyCo() + cCompanys.getCpyChico());
                        resMap.put("id", cCompanys.getId());
                        coList.add(resMap);
                    }
                }
                trucksVo.setCoList(coList);
            }
            trucksVo.setTrucksCheckinBatchSn(cTrucks.getTrucksCheckinBatchSn());
            trucksVo.setTrucksCheckinBatch(cTrucks.getTrucksCheckinBatch());
            trucksVo.setTrucksCheckinLoadsite(cTrucks.getTrucksCheckinLoadsite());
//            trucksPreDeduct
            trucksVo.setTrucksPreDeduct(cTrucks.getTrucksPreDeduct());
//            trucksBillGross
            trucksVo.setTrucksBillGross(cTrucks.getTrucksBillGross());
//trucksBillTare
            trucksVo.setTrucksBillTare(cTrucks.getTrucksBillTare());
            //trucksBillNet
            trucksVo.setTrucksBillNet(cTrucks.getTrucksBillNet());
            //trucksDepStation
            trucksVo.setTrucksDepStation(cTrucks.getTrucksDepStation());
            //trucksBoat
            trucksVo.setTrucksBoat(cTrucks.getTrucksBoat());
            //trucksShip
            trucksVo.setTrucksShip(cTrucks.getTrucksShip());
            //trucksMineral
            trucksVo.setTrucksMineral(cTrucks.getTrucksMineral());
            //trucksCoaltype
            trucksVo.setTrucksCoaltype(cTrucks.getTrucksCoaltype());
//            trucksShipTm
            trucksVo.setTrucksShipTm(cTrucks.getTrucksShipTm());
            //trucksDelyTm
            trucksVo.setTrucksDelyTm(cTrucks.getTrucksDelyTm());
            //trucksCheckinTm
            trucksVo.setTrucksCheckinTm(cTrucks.getTrucksCheckinTm());
//            trucksCallinTm

            trucksVo.setTrucksCallinTm(cTrucks.getTrucksCallinTm());
//            trucksEnterTm
            trucksVo.setTrucksEnterTm(cTrucks.getTrucksEnterTm());
            //trucksPoundinTm
            trucksVo.setTrucksPoundinTm(cTrucks.getTrucksPoundinTm());
            //trucksPoundoutTm
            trucksVo.setTrucksPoundoutTm(cTrucks.getTrucksPoundoutTm());
            //trucksCheckinOperator
            trucksVo.setTrucksCheckinOperator(cTrucks.getTrucksCheckinOperator());
            //trucksRegMan
            trucksVo.setTrucksRegMan(cTrucks.getTrucksRegMan());
            //trucksFixMan
            trucksVo.setTrucksFixMan(cTrucks.getTrucksFixMan());
            //trucksFixTm
            trucksVo.setTrucksFixTm(cTrucks.getTrucksFixTm());
            //trucksQueuePreStatus
            trucksVo.setTrucksQueuePreStatus(cTrucks.getTrucksQueuePreStatus());
            //trucksQueueStatus
            trucksVo.setTrucksQueueStatus(cTrucks.getTrucksQueueStatus());
            //trucksSampling
            trucksVo.setTrucksSampling(cTrucks.getTrucksSampling());
            //gpsDeviceNo
            trucksVo.setGpsDeviceNo(cTrucks.getTrucksGpsDevNo());
            //svrTmstamp
            trucksVo.setSvrTmstamp(cTrucks.getSvrTmstamp());
            //cliTmstamp
            trucksVo.setCliTmstamp(cTrucks.getCliTmstamp());
            //trucksCheckinIdentied
            trucksVo.setTrucksCheckinIdentied(cTrucks.getTrucksCheckinIdentied());
            //trucksEntered
            trucksVo.setTrucksEntered(cTrucks.getTrucksEntered());
            //trucksBreakdown
            trucksVo.setTrucksBreakdown(cTrucks.getTrucksBreakdown());
            //trucksLinger
            trucksVo.setTrucksLinger(cTrucks.getTrucksLinger());
            //trucksRefTareInited
            trucksVo.setTrucksRefTareInited(cTrucks.getTrucksRefTareInited());
            //trucksMustQueue
            trucksVo.setTrucksMustQueue(cTrucks.getTrucksMustQueue());
            //trucksIdentyQueue
            trucksVo.setTrucksIdentyQueue(cTrucks.getTrucksIdentyQueue());
            //trucksIdentyLoad
            trucksVo.setTrucksIdentyLoad(cTrucks.getTrucksIdentyLoad());
            //trucksInfoApproved
            trucksVo.setTrucksInfoApproved(cTrucks.getTrucksInfoApproved());
            //trucksDeleted
            trucksVo.setTrucksDeleted(cTrucks.getTrucksDeleted());
            //trucksNew
            trucksVo.setTrucksNew(cTrucks.getTrucksNew());
//            downedID1
            trucksVo.setDownedID1(cTrucks.getDownedID1());
//            downedID2
            trucksVo.setDownedID2(cTrucks.getDownedID2());
//            downedID3
            trucksVo.setDownedID3(cTrucks.getDownedID3());
//            downedID4
            trucksVo.setDownedID4(cTrucks.getDownedID4());
//            downedID5
            trucksVo.setDownedID5(cTrucks.getDownedID5());
//            downedID6
            trucksVo.setDownedID6(cTrucks.getDownedID6());
//            downedID7
            trucksVo.setDownedID7(cTrucks.getDownedID7());
//            downedID8
            trucksVo.setDownedID8(cTrucks.getDownedID8());
//            downedID9
            trucksVo.setDownedID9(cTrucks.getDownedID9());
//            downedID10
            trucksVo.setDownedID10(cTrucks.getDownedID10());
//            downedID11
            trucksVo.setDownedID11(cTrucks.getDownedID11());
//            downedID12
            trucksVo.setDownedID12(cTrucks.getDownedID12());
//            downedID13
            trucksVo.setDownedID13(cTrucks.getDownedID13());
//            downedID14
            trucksVo.setDownedID14(cTrucks.getDownedID14());
//            downedID15
            trucksVo.setDownedID15(cTrucks.getDownedID15());
//            downedID16
            trucksVo.setDownedID16(cTrucks.getDownedID16());
//            downedID17
            trucksVo.setDownedID17(cTrucks.getDownedID17());
//            downedID18
            trucksVo.setDownedID18(cTrucks.getDownedID18());
//            downedID19
            trucksVo.setDownedID19(cTrucks.getDownedID19());
//            downedID20
            trucksVo.setDownedID20(cTrucks.getDownedID20());
//            dataFrom
            trucksVo.setDataFrom(cTrucks.getDataFrom());
//            serialNo
            trucksVo.setSerialNo(cTrucks.getSerialNo());

            //            如果车辆是等待状态就不显示装载点等信息
            if (cTrucks.getTrucksQueueStatus().equals(BigDecimal.ONE)) {
                trucksVo.setTrucksCallinTm(null);
                trucksVo.setTrucksCheckinLoadsite(null);
                trucksVo.setSpecialPrice(null);
            }
            trucksVos.add(trucksVo);
        }
        map.put("list", trucksVos);
        return SzResult.success(map);
    }
    /**
     * 新增车辆排队信息
     *
     * @param CardInfCardNo 卡号
     * @param truckNo       车牌号
     * @param queuePdtId    排队品种id
     */
    public SzResult addWaitTrucks(String CardInfCardNo, String truckNo, String queuePdtId, String co, String chiCo, String token, String plantId) throws Exception {
        AddTrucksQueueVo addTrucksQueueVo = new AddTrucksQueueVo();
        //根据卡号查询该IC卡的信息
        QueryWrapper<CIcCardsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CARDINF_CARDNO", CardInfCardNo);
        queryWrapper.eq("CARDINF_DELETED", 0);
        queryWrapper.eq("CARDINF_REPORT_LOST", 0);
        //CARDINF_MODIFY_REASON为解挂或者发卡
        queryWrapper.and(i -> i.eq("CARDINF_MODIFY_REASON", "解挂").or().eq("CARDINF_MODIFY_REASON", "发卡"));
        queryWrapper.eq("DATA_FROM", plantId);
        CIcCardsInfo icCardsInfo = iCIcCardsInfoService.getOne(queryWrapper);
        if (icCardsInfo == null) {
            return SzResult.error("该IC卡不存在");
        }
        if (icCardsInfo.getCardinfCurValid() == 0) {
            return SzResult.error("该IC卡已注销或挂失");
        }
        QueryWrapper<CTrucks> trucksQueryWrapper = new QueryWrapper<>();
        trucksQueryWrapper.eq("TRUCKS_CHECKIN_CARDNO", icCardsInfo.getCardinfCardno());
        trucksQueryWrapper.ne("TRUCKS_QUEUE_STATUS", 0);
        trucksQueryWrapper.eq("TRUCKS_DELETED", 0);
        trucksQueryWrapper.eq("DATA_FROM", plantId);
        CTrucks cTrucks1 = iCTrucksService.getOne(trucksQueryWrapper);
        if (cTrucks1 != null) {
            return SzResult.error("该IC卡已经使用排队");
        }
        addTrucksQueueVo.setCardInfCardNo(CardInfCardNo);
        String cardInfType = icCardsInfo.getCardinfType();//IC卡类型，附卡（绑定车号）、附卡（绑定密码）、进货卡（绑定车号）、进货卡（绑定密码）
        String cardInfTruckPwd = icCardsInfo.getCardinfTruckPwd();//车号
        if (cardInfType.contains("绑定车号")) {//车号已固定，不能修改
            QueryWrapper<CTrucks> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("TRUCKS_TRUCKNO", cardInfTruckPwd);
            queryWrapper1.eq("TRUCKS_DELETED", 0);
            queryWrapper1.eq("DATA_FROM", plantId);
            CTrucks cTrucks = iCTrucksService.getOne(queryWrapper1);//根据车号查询车辆是否存在
            if (cTrucks == null) {//车辆不存在
                return SzResult.error("此卡绑定的车号不存在");
            }
            addTrucksQueueVo.setTruckNo(cardInfTruckPwd);
        }
        //cardInfType包含绑定密码或者临时灰卡
        if (cardInfType.contains("绑定密码") || cardInfType.contains("临时灰卡")) {
            if (truckNo != null) {
                QueryWrapper<CTrucks> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("TRUCKS_TRUCKNO", truckNo);
                queryWrapper1.eq("TRUCKS_DELETED", 0);
                queryWrapper1.eq("DATA_FROM", plantId);
                CTrucks cTrucks = iCTrucksService.getOne(queryWrapper1);//根据车号查询车辆是否存在
                if (cTrucks == null) {//车辆不存在
                    return SzResult.error("此卡绑定的车号不存在");
                }
                addTrucksQueueVo.setTruckNo(truckNo);
            } else {
                return SzResult.error("车号不能为空");
            }
        }
        //车号新增完成

        //判断该IC卡是否为代扣卡
        String cardInfAgent = String.valueOf(icCardsInfo.getCardinfAgent());//是否为代扣卡
        if (cardInfAgent.equals("1")) {//是代扣卡,那排队单位为从单位中自行选择
            QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("CPY_CO", co);
            queryWrapper1.eq("CPY_CHICO", chiCo);
            queryWrapper1.eq("CPY_DELETED", 0);
            queryWrapper1.eq("CPY_PASS", 0);
            queryWrapper1.eq("CPY_TEMP_LOCK", 0);
            queryWrapper1.eq("DATA_FROM", plantId);
            CCompanys cCompanys = iCCompanysService.getOne(queryWrapper1);
            if (cCompanys == null) {//单位不存在
                return SzResult.error("该单位已被冻结或作古");
            }
            addTrucksQueueVo.setQueueCo(co);
            addTrucksQueueVo.setQueueZhiCo(chiCo);
        } else {//不是代扣卡，那排队单位为IC卡的收货单位或者是供货单位
            //通过卡号查询该IC卡的收货单位
            QueryWrapper<CIcCardsInfo> queryWrapper1 = new QueryWrapper<>();
            String cardinfCardno = icCardsInfo.getCardinfCardno();
            queryWrapper1.eq("CARDINF_CARDNO", cardinfCardno);
            queryWrapper1.eq("DATA_FROM", plantId);
            queryWrapper1.ne("CARDINF_MODIFY_REASON", "注销");
            CIcCardsInfo icCardsInfo1 = iCIcCardsInfoService.getOne(queryWrapper1);
            if (icCardsInfo1 == null) {
                return SzResult.error("该IC卡不存在");
            }
            String cardInfCo = icCardsInfo1.getCardinfCo();//收货单位
            if (cardInfCo == null) {//收货单位为空，那排队单位为供货单位
                String cardInfCoSupply = icCardsInfo1.getCardinfCoSupply();//供货单位
                String cardInfChicoSupply = icCardsInfo1.getCardinfChicoSupply();//供货子单位
                addTrucksQueueVo.setQueueCo(cardInfCoSupply);
                addTrucksQueueVo.setQueueZhiCo(cardInfChicoSupply);
            } else {//收货单位不为空，那排队单位为收货单位
                addTrucksQueueVo.setQueueCo(cardInfCo);
                addTrucksQueueVo.setQueueZhiCo(icCardsInfo1.getCardinfChiCo());
            }
        }
        //排队单位新增完成

        //排队品种
        String cardinfPreferPdt = icCardsInfo.getCardinfPreferPdt();
        if (cardinfPreferPdt == null || cardinfPreferPdt.equals("")) {//该IC卡未绑定品种
            //司机自己选品种
            QueryWrapper<CProductsInfo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("PDTINF_DELETED", 0);
            queryWrapper1.eq("ID", queuePdtId);
            queryWrapper1.eq("DATA_FROM", plantId);
            CProductsInfo cProductsInfo = iCProductsInfoService.getOne(queryWrapper1);
            if (cProductsInfo == null) {
                return SzResult.error("该品种不存在");
            }
            Integer pdtInfQueue = cProductsInfo.getPdtinfQueue();
            if (pdtInfQueue == null) {
                return SzResult.error("该品种不允许排队");
            }
            if (pdtInfQueue != 1) {
                return SzResult.error("该品种不允许排队");
            }
            QueryWrapper<CPdtsAuth> queryWrapper2 = new QueryWrapper<>();
            //  queryWrapper2.eq("PDTSAUTH_ID", new BigDecimal(queuePdtId));
            queryWrapper2.eq("PDTSAUTH_PDT", cProductsInfo.getPdtinfPdt());
            queryWrapper2.eq("PDTSAUTH_CO", icCardsInfo.getCardinfCo());
            queryWrapper2.eq("PDTSAUTH_CHICO", icCardsInfo.getCardinfChiCo());
            queryWrapper2.eq("PDTSAUTH_DELETED", 0);
            queryWrapper2.eq("DATA_FROM", plantId);
            List<CPdtsAuth> cPdtsAuths = iCPdtsAuthService.list(queryWrapper2);
            if (cPdtsAuths.size() == 0) {
                return SzResult.error("该IC卡绑定的收货单位没有该品种的权限");
            }
            addTrucksQueueVo.setQueuePdtId(queuePdtId);
            BigDecimal pdtInfSn = cProductsInfo.getPdtinfSn();
            addTrucksQueueVo.setQueuePdtNum(String.valueOf(pdtInfSn));
        } else {// 该IC卡已绑定品种
            QueryWrapper<CProductsInfo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("PDTINF_DELETED", 0);
            queryWrapper1.eq("PDTINF_PDT", cardinfPreferPdt);
            queryWrapper1.eq("DATA_FROM", plantId);
            CProductsInfo cProductsInfo = iCProductsInfoService.getOne(queryWrapper1);
            if (cProductsInfo == null) {
                return SzResult.error("该IC卡绑定的品种不存在");
            }
            String id = cProductsInfo.getId();
            addTrucksQueueVo.setQueuePdtId(id);
            BigDecimal pdtInfSn = cProductsInfo.getPdtinfSn();
            addTrucksQueueVo.setQueuePdtNum(String.valueOf(pdtInfSn));
        }
        //排队品种新增和品种编号完成
        //登记操作员
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String userName = user.getTrueName();
        addTrucksQueueVo.setQueueOperator(userName);
        String truckNo1 = addTrucksQueueVo.getTruckNo();
        //(truckNo=鲁A00001, CardInfCardNo=543534, queueCo=泰州电厂, queueZhiCo=，, queuePdtId=25, queuePdtNum=25, queueOperator=9)
        //根据车牌号查询车辆信息
        QueryWrapper<CTrucks> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("TRUCKS_DELETED", 0);
        queryWrapper2.eq("TRUCKS_TRUCKNO", truckNo1);
        queryWrapper2.eq("DATA_FROM", plantId);
        CTrucks cTrucks = iCTrucksService.getOne(queryWrapper2);
        if (cTrucks == null) {
            return SzResult.error("该车辆不存在");
        }
        cTrucks.setTrucksCheckinCardno(CardInfCardNo);
        String queueCo = addTrucksQueueVo.getQueueCo();
        QueryWrapper<CCompanys> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("CPY_DELETED", 0);
        queryWrapper3.eq("CPY_CO", queueCo);
        queryWrapper3.eq("DATA_FROM", plantId);
        CCompanys cCompanys = iCCompanysService.getOne(queryWrapper3);
        String cpyId = String.valueOf(cCompanys.getId());
        cTrucks.setTrucksCheckinCo(cpyId);
        //String queuePdtId1 = addTrucksQueueVo.getQueuePdtId();
        if (addTrucksQueueVo.getQueuePdtId() == null) {
            cTrucks.setTrucksCheckinPdt(null);
        } else {
            cTrucks.setTrucksCheckinPdt(addTrucksQueueVo.getQueuePdtId());
        }
//        cTrucks.setTrucksCheckinPdtsn(BigDecimal.valueOf(Long.parseLong(addTrucksQueueVo.getQueuePdtNum())));
        if (addTrucksQueueVo.getQueuePdtNum() == null) {
            cTrucks.setTrucksCheckinPdtsn(null);
        } else {
            cTrucks.setTrucksCheckinPdtsn(BigDecimal.valueOf(Long.parseLong(addTrucksQueueVo.getQueuePdtNum())));
        }
        cTrucks.setTrucksQueueStatus(BigDecimal.valueOf(1));
        cTrucks.setTrucksQueuePreStatus(BigDecimal.valueOf(0));
        cTrucks.setTrucksCheckinTm(LocalDateTime.now());
        iCTrucksService.updateById(cTrucks);
        return SzResult.Success();
    }


    //判断该IC卡是绑定车号还是绑定品种。
    public SzResult IcCardJudge(String CardInfCardNo, String plantId) {
        Map<String, Object> map = new HashMap<>();
        //根据IC卡号查询IC卡信息
        QueryWrapper<CIcCardsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CARDINF_DELETED", 0);
        queryWrapper.eq("CARDINF_CARDNO", CardInfCardNo);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.ne("CARDINF_MODIFY_REASON", "注销");
        CIcCardsInfo cIcCardsInfo = iCIcCardsInfoService.getOne(queryWrapper);
        if (cIcCardsInfo == null) {
            return SzResult.success("该IC卡不存在");
        } else {
            String cardInfType = cIcCardsInfo.getCardinfType();
            String cardInfPreferPdt = cIcCardsInfo.getCardinfPreferPdt();
            BigDecimal cardinfAgent = cIcCardsInfo.getCardinfAgent();
            if (cardInfType.contains("绑定密码") || cardInfType.contains("临时灰卡")) {
                map.put("type", cardInfType);
            }
            if (cardInfType.contains("绑定车号")) {
                map.put("type", cardInfType);
            }
            if (cardInfPreferPdt == null) {
                map.put("ifPdt", "未绑定品种");
            } else {
                map.put("ifPdt", "绑定品种");
            }
            if (cardinfAgent.intValue() == 1) {
                map.put("ifAgent", "是代扣卡");//自己选择单位
            } else {
                map.put("ifAgent", "不是代扣卡");
                String cardinfCo = cIcCardsInfo.getCardinfCo();
                String cardinfChiCo = cIcCardsInfo.getCardinfChiCo();
                map.put("cardinfCo", cardinfCo);
                map.put("cardinfChiCo", cardinfChiCo);
            }
        }

        return SzResult.success(map);
    }


    /**
     * 修改排队车辆状态
     *
     * @return
     */
    public SzResult upDateWaitTrucks(String token, String id, String targetStatus, String plantId) throws Exception {
        //根据id查询车辆信息
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        //queryWrapper.ne("TRUCKS_QUEUE_STATUS", 0);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        queryWrapper.eq("TRUCKS_ID", id);
        queryWrapper.eq("DATA_FROM", plantId);
        CTrucks cTrucks = iCTrucksService.getOne(queryWrapper);
        if (cTrucks == null) {
            return SzResult.error("该车辆不存在");
        }

        //cunzai
        //当前状态（0已出厂、1等待、2请进、3已进、4预备 5正装 6装完 ）
        BigDecimal trucksQueueStatus = cTrucks.getTrucksQueueStatus();
        if (targetStatus != null) {
            int currentStatus = trucksQueueStatus.intValue();
            int newStatus = Integer.parseInt(targetStatus);

            // 确保不能从大于等于3的状态变更为小于3的状态
            if (currentStatus >= 3 && newStatus < 3) {
                return SzResult.error("不可从大于等于【已进】的状态变更为小于【已进】的状态");
            }

            // 确保不能从小于或等于2的状态变更为大于2的状态
            if (currentStatus <= 2 && newStatus > 2) {
                return SzResult.error("不可从小于或等于【请进】的状态变更为大于【请进】的状态");
            }

            // 更新状态
            cTrucks.setTrucksQueueStatus(BigDecimal.valueOf(newStatus));

            // 如果当前状态是请进（值为2），记录调用时间
            if (currentStatus == 2){
                cTrucks.setTrucksCallinTm(LocalDateTime.now());
            }

            // 设置前一个状态
            cTrucks.setTrucksQueuePreStatus(BigDecimal.valueOf(currentStatus));
        }
        if (targetStatus != null && Integer.parseInt(targetStatus) == 0) {

            cTrucks.setTrucksQueueStatus(BigDecimal.ZERO);
            cTrucks.setTrucksQueueVip(0);
            cTrucks.setTrucksCheckinNo(null);
            cTrucks.setTrucksCallinTm(null);
            cTrucks.setTrucksCheckinTm(null);
            cTrucks.setTrucksCheckinPdt("");
            cTrucks.setTrucksCheckinCo("");
            cTrucks.setTrucksCheckinLoadsite("");
            cTrucks.setTrucksCheckinLoadsiteSn(null);
            cTrucks.setTrucksCheckinCardno("");
            cTrucks.setTrucksCheckinLoadsiteAdd("");
            cTrucks.setSupplyCoId("");
        }
        QueryWrapper<CTrucksUpdate> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("TRUCKS_ID", cTrucks.getTrucksId());
        queryWrapper1.eq("DATA_FROM", plantId);
        queryWrapper1.eq("HANDLED_RESULT",0);
        List<CTrucksUpdate> list = iCTrucksUpdateService.list(queryWrapper1);
        for (CTrucksUpdate cTrucksUpdate : list) {
            cTrucksUpdate.setHandledResult(100);
            cTrucksUpdate.setHandled(String.valueOf(22222222));
            cTrucksUpdate.setHandledResultStr("回复：取消排队");
            iCTrucksUpdateService.updateById(cTrucksUpdate);
        }
        iCTrucksService.updateById(cTrucks);
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        //  String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        String string = trucksQueueStatus.toString();
        //当string为0时，targetStatus为已出厂，当string为1时，targetStatus为等待，当string为2时，targetStatus为请进，当string为3时，targetStatus为已进，当string为4时，targetStatus为预备，当string为5时，targetStatus为正装，当string为6时，targetStatus为装完
        if (string.equals("0")) {
            string = "已出厂";
        } else if (string.equals("1")) {
            string = "等待";
        } else if (string.equals("2")) {
            string = "请进";
        } else if (string.equals("3")) {
            string = "已进";
        } else if (string.equals("4")) {
            string = "预备";
        } else if (string.equals("5")) {
            string = "正装";
        } else if (string.equals("6")) {
            string = "装完";
        }
        //当targetStatus为0时，string为已出厂，当targetStatus为1时，string为等待，当targetStatus为2时，string为请进，当targetStatus为3时，string为已进，当targetStatus为4时，string为预备，当targetStatus为5时，string为正装，当targetStatus为6时，string为装完
        if (targetStatus.equals("0")) {
            targetStatus = "已出厂";
        } else if (targetStatus.equals("1")) {
            targetStatus = "等待";
        } else if (targetStatus.equals("2")) {
            targetStatus = "请进";
        } else if (targetStatus.equals("3")) {
            targetStatus = "已进";
        } else if (targetStatus.equals("4")) {
            targetStatus = "预备";
        } else if (targetStatus.equals("5")) {
            targetStatus = "正装";
        } else if (targetStatus.equals("6")) {
            targetStatus = "装完";
        }
        sysLogBu.logRecord("修改排队车辆状态", sysUser.getTrueName(), "车牌号:【" + cTrucks.getTrucksTruckno() + "】的排队状态" + "由【" + string + "】" + "改为" + "【" + targetStatus + "】", plantId);
        return SzResult.Success();
    }


    //查询车辆排队记录
    public SzResult getWaitTrucksRecord(String truckNo, String cardNo, String cpyId, String page, String pageSize, String plantId, String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        Page<CTrucksUpdate> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
        QueryWrapper<CTrucksUpdate> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.like(truckNo != null && !truckNo.isEmpty(), "TRUCKS_TRUCKNO", truckNo);
        queryWrapper1.like(cardNo != null && !cardNo.isEmpty(), "TRUCKS_CHECKIN_CARDNO", cardNo);
        queryWrapper1.ge(StringUtils.isNotEmpty(startTime), "HANDLED5_TIME", startTime);
        queryWrapper1.le(StringUtils.isNotEmpty(endTime), "HANDLED5_TIME", endTime);
        queryWrapper1.eq(cpyId != null && !cpyId.isEmpty(), "TRUCKS_CHECKIN_CO", cpyId);
        queryWrapper1.eq("DATA_FROM", plantId);
        queryWrapper1.orderByDesc("HANDLED_TIME");
        IPage<CTrucksUpdate> cTrucksIPage = iCTrucksUpdateService.page(page1, queryWrapper1);
        List<CTrucksUpdate> list = cTrucksIPage.getRecords();
        long total = cTrucksIPage.getTotal();
        map.put("total", total);
        List<CTrucksUpdateVo> cTrucksUpdateVo = list.stream()
                .map(cTrucksUpdate -> {
                    CTrucksUpdateVo trucksUpdateVo = new CTrucksUpdateVo();
                    trucksUpdateVo.setId(cTrucksUpdate.getId());
                    trucksUpdateVo.setTrucksId(String.valueOf(cTrucksUpdate.getTrucksId()));
                    trucksUpdateVo.setTrucksTruckno(cTrucksUpdate.getTrucksTruckno());//车号
                    trucksUpdateVo.setTrucksCheckinCo(cTrucksUpdate.getTrucksCheckinCo());//单位id
                    trucksUpdateVo.setTrucksCheckinCoName(getCompanyName(cTrucksUpdate.getTrucksCheckinCo(), plantId));//单位名称
                    trucksUpdateVo.setTrucksCheckinCardno(cTrucksUpdate.getTrucksCheckinCardno());//卡号
                    trucksUpdateVo.setTrucksCheckinPdt(cTrucksUpdate.getTrucksCheckinPdt());//品种id
                    trucksUpdateVo.setTrucksCheckinPdtName(getProductName(cTrucksUpdate.getTrucksCheckinPdt(), plantId));//品种名称

                    trucksUpdateVo.setTrucksCheckinLoadsite(cTrucksUpdate.getTrucksCheckinLoadsite());
                    trucksUpdateVo.setTrucksCheckinDriver(cTrucksUpdate.getTrucksCheckinDriver());
                    trucksUpdateVo.setTrucksCheckinDriverPhone(cTrucksUpdate.getTrucksCheckinDriverPhone());
                    trucksUpdateVo.setUpdateType(cTrucksUpdate.getUpdateType());//提交类型，5排队，6取消排队
                    trucksUpdateVo.setHandledResultStr(cTrucksUpdate.getHandledResultStr());//处理结果
                    trucksUpdateVo.setDataFrom(cTrucksUpdate.getDataFrom());
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    // 假设 formatter 已经被定义并初始化
                    Optional.ofNullable(cTrucksUpdate.getHandled5Time())
                            .map(time -> time.format(formatter))
                            .ifPresent(trucksUpdateVo::setHandledTime);

                    Optional.ofNullable(cTrucksUpdate.getHandled6Time())
                            .map(time -> time.format(formatter))
                            .ifPresent(trucksUpdateVo::setCancelHandledTime);
                    return trucksUpdateVo;
                })
                .collect(Collectors.toList());
        map.put("list", cTrucksUpdateVo);

        return SzResult.success(map);
    }


    //删除车辆排队记录
    public SzResult deleteWaitTrucksRecord(String id, String plantId) {
        //根据id直接删除
        iCTrucksUpdateService.removeById(id);
        return SzResult.Success();
    }


    //根据id查询单位名称
    public String getCompanyName(String id, String plantId) {
        QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CPY_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("ID", id);
        CCompanys cCompanys = iCCompanysService.getOne(queryWrapper);
        if (cCompanys == null) {
            return "";
        }
        return cCompanys.getCpyCo() + "." + cCompanys.getCpyChico();
    }

    //根据id查询品种名称
    public String getProductName(String id, String plantId) {
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PDTINF_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("ID", id);
        CProductsInfo cProductsInfo = iCProductsInfoService.getOne(queryWrapper);
        if (cProductsInfo == null) {
            return "";
        }
        return cProductsInfo.getPdtinfPdt();
    }

    //暂且不用
    public SzResult startQueueByTruckNo(String token, Map map, String plant) throws Exception {
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
//        排队品种  cardinfTruckTypeValue
        String cardinfTruckTypeValue = map.get("cardinfTruckTypeValue").toString();
//        排队卡号 cardauthCardno
//        排队车号 cardinfTruckPwd
        String cardinfTruckPwd = map.get("cardinfTruckPwd").toString();
//        查询这个车号的代理卡
        List<CIcCardsInfo> cIcCardsInfos = cIcCardsInfoMapper.selectIcAgentByTruckNo(plant, cardinfTruckPwd);
        String cardauthCardno = "";
        if (cIcCardsInfos.size() == 1) {
            cardauthCardno = cIcCardsInfos.get(0).getCardinfCardno();

        } else if (cIcCardsInfos.size() > 1) {
            return SzResult.Error("该车辆有多个代理卡，请联系管理员");
        } else {
            return SzResult.Error("该车辆没有代理卡，请联系管理员");
        }
//        卸货点 cardinfTruckName
        String cardinfTruckName = "";
        if (map.get("cardinfTruckName") != null && !map.get("cardinfTruckName").equals("")) {
            cardinfTruckName = map.get("cardinfTruckName").toString();
        } else {
            cardinfTruckName = "";
        }
        //        排队司机姓名 cardauthDriverUserName
        String cardauthDriverUserName = user.getTrueName();
//    司机手机号 cardauthDriverPhone
        String cardauthDriverPhone = "";
        if (user.getPhone() != null) {
            cardauthDriverPhone = user.getPhone();
        }
//        收货单位 cardinfCompany
        String cardinfCompany = map.get("cardinfCompany").toString();
//        收货单位id：cardinfCompanyId
        //cardinfCompany 根据.分割
        String cardinfCompanyCo = cardinfCompany.split("\\.")[0];
        String cardinfCompanyChiCo = cardinfCompany.split("\\.")[1];
//        if (cardinfCompanyCo.equals(plantName.split("\\.")[0]) && cardinfCompanyChiCo.equals(plantName.split("\\.")[1])) {
//            map1.put("code", 1);
//            map1.put("msg", "收货单位和供货单位不能相同");
//            return map1;
//        }
        QueryWrapper<CCompanys> cCompanysQueryWrapper = new QueryWrapper<>();
        cCompanysQueryWrapper.eq("CPY_CO", cardinfCompanyCo);
        cCompanysQueryWrapper.eq("CPY_CHICO", cardinfCompanyChiCo);
        cCompanysQueryWrapper.eq("CPY_DELETED", 0);
        cCompanysQueryWrapper.eq("DATA_FROM", plant);
        CCompanys cCompanys = iCCompanysService.getOne(cCompanysQueryWrapper);
        String cardinfCompanyId = cCompanys.getId();
//检查卡是否正常
        int isOk = cIcCardsInfoMapper.selectCountAgent(plant, cardauthCardno);
//        }

        if (isOk == 0) {
            return SzResult.Error("此卡已注销或挂失");
        }
//        查看是否是这个司机的卡
//        if (!cardauthDriverPhone.equals("")) {
//            if (!cardauthDriverPhone.equals(userInfo.getUser().getPhone())) {
//                map1.put("code", 1);
//                map1.put("msg", "此卡不是您的卡,可能是您登录了其他账号后切换回自己的账号的遗留数据，请重新登录后再试");
//                return map1;
//            }
//        }
        QueryWrapper<CTrucks> cTrucksQueryWrapper = new QueryWrapper<>();
        cTrucksQueryWrapper.eq("TRUCKS_TRUCKNO", cardinfTruckPwd);
        cTrucksQueryWrapper.eq("TRUCKS_DELETED", 0);
        cTrucksQueryWrapper.eq("DATA_FROM", plant);
        CTrucks cTrucks = iCTrucksService.getOne(cTrucksQueryWrapper);
        if (cTrucks == null) {
            return SzResult.Error("车号不在车库中，请联系管理员添加车号");
        }
        try {
            QueryWrapper<CTrucksUpdate> cTrucksUpdateQueryWrapper9 = new QueryWrapper<>();
            cTrucksUpdateQueryWrapper9.eq("TRUCKS_TRUCKNO", cardinfTruckPwd);
            cTrucksUpdateQueryWrapper9.eq("TRUCKS_DELETED", 0);
            cTrucksUpdateQueryWrapper9.eq("UPDATE_TYPE", 5);
            cTrucksUpdateQueryWrapper9.eq("TRUCKS_NEW", 1);
//            HANDLED_TIME一天之内
            cTrucksUpdateQueryWrapper9.ge("HANDLED_TIME", LocalDateTime.now().truncatedTo(ChronoUnit.DAYS));
//            根据HANDLED_TIME查到最新的一条
            cTrucksUpdateQueryWrapper9.orderByDesc("HANDLED_TIME");
            cTrucksUpdateQueryWrapper9.last("limit 1");
            CTrucksUpdate cTrucksUpdate9 = iCTrucksUpdateService.getOne(cTrucksUpdateQueryWrapper9);
            if (cTrucksUpdate9 != null) {
                if (!cTrucks.getTrucksQueueStatus().toString().equals("0")) {
                    return SzResult.Error("该车辆仍在队列中,由用户" + cTrucksUpdate9.getTrucksCheckinDriver() + "-" + cTrucksUpdate9.getTrucksCheckinDriverPhone() + "排队，请等待完成出厂后再申请排队");
                }
            } else {
                if (cTrucks.getTrucksQueueStatus() != null && !cTrucks.getTrucksQueueStatus().toString().equals("0")) {
                    return SzResult.Error("该车辆仍在队列中,请等待完成出厂后再申请排队");
                }
            }
        } catch (Exception e) {
            if (cTrucks.getTrucksQueueStatus() != null && !cTrucks.getTrucksQueueStatus().toString().equals("0")) {
                return SzResult.Error("该车辆仍在队列中,请等待完成出厂后再申请排队");
            }
        }
//        } catch (Exception e) {
//            map1.put("code", 1);
//            map1.put("msg", "该车辆不允许互联网排队，请联系相关管理员");
//            return map1;
//        }
//        查询品种id
        QueryWrapper<CProductsInfo> productsInfoWrapper = new QueryWrapper<>();
        productsInfoWrapper.eq("PDTINF_PDT", cardinfTruckTypeValue);
        productsInfoWrapper.eq("PDTINF_DELETED", 0);
        productsInfoWrapper.eq("DATA_FROM", plant);
        CProductsInfo productsInfo = iCProductsInfoService.getOne(productsInfoWrapper);
        if (productsInfo.getPdtinfSpeSitelist() != null) {
//            当品种有装载点时cardinfTruckName不能为空
            if (cardinfTruckName == null || cardinfTruckName.equals("")) {
                return SzResult.Error("请选择装卸货点!!!");
            }
        }
        //        判断余额
//        queueHandlingBu.balanceIsEnough(plant, cardauthCardno);如果是false就提示余额不足
//        plant=DATA_FROM \ cardauthCardno=卡号 、cardinfTruckTypeValue=品种名称 、 cardinfTruckPwd=车号
        if (!queueHandlingBu.balanceIsEnough(plant, cardinfCompanyCo, cardinfCompanyChiCo, cardauthCardno, cardinfTruckTypeValue, cardinfTruckPwd)) {
            return SzResult.Error("排队单位余额不足，请充值");
        }
//        判断下限余量
        Map<String, Double> isLowerLimit = queueHandlingBu.isLowerLimit(plant, cardinfCompanyCo, cardinfCompanyChiCo, cardinfTruckTypeValue, cardauthCardno);
        if (isLowerLimit.get("ok") == 0.0) {
            return SzResult.Error("排队单位余额不足装" + isLowerLimit.get("limit").toString() + "吨");
        }
//        判断是否还有计划
        boolean isPlan = queueHandlingBu.isPlan(plant, cardinfCompanyCo, cardinfCompanyChiCo, cardinfTruckTypeValue, cardauthCardno, cardinfTruckPwd);
        if (!isPlan) {
            return SzResult.Error("无计划或计划已用完了");
        }
//        =============================至此开始排队==================================================
//        查询管理员是否开启手机排队
        QueryWrapper<SysDic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DIC_TYPE", "queue_phone");
        queryWrapper.eq("DIC_KEY", "queueControl");
        queryWrapper.eq("DATA_FROM", plant);
//        拿到结果
        SysDic sysDic = sysDicService.getOne(queryWrapper);

        QueryWrapper<CTrucksUpdate> cTrucksUpdateQueryWrapper = new QueryWrapper<>();
        cTrucksUpdateQueryWrapper.eq("TRUCKS_TRUCKNO", cardinfTruckPwd);
        cTrucksUpdateQueryWrapper.eq("TRUCKS_DELETED", 0);
        cTrucksUpdateQueryWrapper.eq("TRUCKS_NEW", 1);
//    HANDLED的值为字符串******，里面存在0，就说明还没处理
        cTrucksUpdateQueryWrapper.like("HANDLED", "0");
        CTrucksUpdate cTrucksUpdate1 = iCTrucksUpdateService.getOne(cTrucksUpdateQueryWrapper);
        if (cTrucksUpdate1 != null) {
            String[] split = cTrucksUpdate1.getHandled().split("");
            for (String s : split) {
                if (s.equals("0") || s.equals("1")) {
                    return SzResult.Error("该车辆排队申请中，申请人:" + cTrucksUpdate1.getTrucksCheckinDriver() + "-" + cTrucksUpdate1.getTrucksCheckinDriverPhone() + "，请勿重复申请排队");
                }
            }
        }
        QueryWrapper<CTrucksUpdate> cTrucksUpdateQueryWrapper1 = new QueryWrapper<>();
        cTrucksUpdateQueryWrapper1.eq("TRUCKS_CHECKIN_CARDNO", cardauthCardno);
        cTrucksUpdateQueryWrapper1.eq("TRUCKS_DELETED", 0);
//                cTrucksUpdateQueryWrapper1.eq("HANDLED", 0);
//             HANDLED_RESULT小于1
        cTrucksUpdateQueryWrapper1.le("HANDLED_RESULT", 1);
        List<CTrucksUpdate> cTrucksUpdates = iCTrucksUpdateService.list(cTrucksUpdateQueryWrapper1);
        if (cTrucksUpdates.isEmpty()) {
            QueryWrapper<CTrucks> cTrucksQueryWrapper3 = new QueryWrapper<>();
            cTrucksQueryWrapper3.eq("TRUCKS_CHECKIN_CARDNO", cardauthCardno);
            cTrucksQueryWrapper3.ne("TRUCKS_QUEUE_STATUS", 0);
            cTrucksQueryWrapper3.eq("TRUCKS_DELETED", 0);
            List<CTrucks> cTrucks3 = iCTrucksService.list(cTrucksQueryWrapper3);
            if (!cTrucks3.isEmpty()) {
                QueryWrapper<CTrucksUpdate> cTrucksUpdateQueryWrapper10 = new QueryWrapper<>();
                cTrucksUpdateQueryWrapper10.eq("TRUCKS_TRUCKNO", cardinfTruckPwd);
                cTrucksUpdateQueryWrapper10.eq("TRUCKS_DELETED", 0);
                cTrucksUpdateQueryWrapper10.eq("UPDATE_TYPE", 5);
                cTrucksUpdateQueryWrapper10.eq("TRUCKS_NEW", 1);
//            HANDLED_TIME一天之内
                cTrucksUpdateQueryWrapper10.ge("HANDLED_TIME", LocalDateTime.now().truncatedTo(ChronoUnit.DAYS));
                cTrucksUpdateQueryWrapper10.last("limit 1");
                CTrucksUpdate cTrucksUpdate10 = iCTrucksUpdateService.getOne(cTrucksUpdateQueryWrapper10);
                if (cTrucksUpdate10 != null) {
                    return SzResult.Error("此车已经被用户：" + cTrucksUpdate10.getTrucksCheckinDriver() + "-" + cTrucksUpdate10.getTrucksCheckinDriverPhone() + "申请排队，请勿重复申请排队");
                } else {
                    return SzResult.Error("此车已经被申请排队，请勿重复申请排队");
                }
            }
            QueryWrapper<CTrucks> cTrucksQueryWrapper1 = new QueryWrapper<>();
            cTrucksQueryWrapper1.eq("TRUCKS_TRUCKNO", cardinfTruckPwd);
            cTrucksQueryWrapper1.eq("TRUCKS_DELETED", 0);
            CTrucks cTrucks1 = iCTrucksService.getOne(cTrucksQueryWrapper1);
//                  如果车辆表中没有这个车号，就返回错误信息
            if (cTrucks1 == null) {
                return SzResult.Error("系统车库内没有该车号,请联系管理员添加");
            } else {

                iCTrucksService.updateById(cTrucks1);
                //        写入排队信息
                CTrucksUpdate cTrucksUpdate = new CTrucksUpdate();
                cTrucksUpdate.setTrucksId(cTrucks.getTrucksId());
                cTrucksUpdate.setTrucksTruckno(cardinfTruckPwd);
                cTrucksUpdate.setTrucksCheckinCardno(cardauthCardno);
                cTrucksUpdate.setTrucksCheckinCo(cardinfCompanyId);
                cTrucksUpdate.setTrucksCheckinPdt(productsInfo.getId());
                cTrucksUpdate.setTrucksCheckinPdtsn(productsInfo.getPdtinfId());
                cTrucksUpdate.setTrucksCheckinLoadsite(cardinfTruckName);
                cTrucksUpdate.setTrucksCheckinDriver(cardauthDriverUserName);
                cTrucksUpdate.setTrucksCheckinDriverPhone(cardauthDriverPhone);
                cTrucksUpdate.setTrucksQueueStatus(BigDecimal.valueOf(0));
                cTrucksUpdate.setHandled5Time(LocalDateTime.now());
                cTrucksUpdate.setHandledTime(LocalDateTime.now());
                cTrucksUpdate.setTrucksDeleted(0);
                cTrucksUpdate.setTrucksNew(1);
                cTrucksUpdate.setUpdateType(5);
//                                    cTrucksUpdate.setUpdateNew(0);
                cTrucksUpdate.setHandledResult(0);
                cTrucksUpdate.setHandled("22220222");
                cTrucksUpdate.setDataFrom(plant);
//                                if (grossWeight != null) {
//                                    cTrucksUpdate.setTrucksBillGross(new BigDecimal(grossWeight));
//                                }
//                                if (tareWeight != null) {
//                                    cTrucksUpdate.setTrucksBillTare(new BigDecimal(tareWeight));
//                                }
//                                if (netWeight != null) {
//                                    cTrucksUpdate.setTrucksBillNet(new BigDecimal(netWeight));
//                                }
//                                cTrucksUpdate.setTrucksBoat(boat);//小船
//                                cTrucksUpdate.setTrucksShip(ship);
//                                cTrucksUpdate.setTrucksCoaltype(coalType);
                //            insert
                iCTrucksUpdateService.save(cTrucksUpdate);
                //      存完拿到id
                String id = cTrucksUpdate.getId();
                //                    往trucks表找到这个车号更新一下，往serialno也写入这个id
                QueryWrapper<CTrucks> cTrucksQueryWrapper2 = new QueryWrapper<>();
                cTrucksQueryWrapper2.eq("TRUCKS_TRUCKNO", cardinfTruckPwd);
                cTrucksQueryWrapper2.eq("TRUCKS_DELETED", 0);
                CTrucks cTrucks2 = iCTrucksService.getOne(cTrucksQueryWrapper2);
                if (cTrucks2 != null) {
//                                        cTrucks2.setSerialNo(id);
                    iCTrucksService.updateById(cTrucks2);
                } else {
                    return SzResult.Error("系统车库内没有该车号,请联系管理员添加");
                }
                //                    排队成功就将之前的记录置为0，也就是变成历史记录
                QueryWrapper<CTrucksUpdate> queryWrapper7 = new QueryWrapper<>();
                queryWrapper7.eq("TRUCKS_ID", cTrucks.getTrucksId());
                queryWrapper7.eq("TRUCKS_NEW", 1);
                queryWrapper7.orderByDesc("HANDLED_TIME");
                List<CTrucksUpdate> cTrucksUpdates1 = iCTrucksUpdateService.list(queryWrapper7);
                if (!cTrucksUpdates1.isEmpty()) {
                    for (int i = 1; i < cTrucksUpdates1.size(); i++) {
                        CTrucksUpdate cTrucksUpdate2 = cTrucksUpdates1.get(i);
                        cTrucksUpdate2.setTrucksNew(0);
                        iCTrucksUpdateService.updateById(cTrucksUpdate2);
                    }
                }
                //            返回结果
                return SzResult.success("申请排队成功，请等待服务器响应");
            }
        } else {
            return SzResult.Error("此卡已经申请排队，请勿重复申请排队");
        }
//                    } else {
//                        map1.put("code", 1);
//                        map1.put("msg", "请在此卡授权时间内使用");
//                        return map1;
//                    }
//                } else {
//                    map1.put("code", 1);
//                    map1.put("msg", "持卡单位已撤销授权，请刷新卡授权信息");
//                    return map1;
//                }
//            } else {
//                map1.put("code", 1);
//                map1.put("msg", "此卡授权信息已被删除，请刷新卡包授权信息");
//                return map1;
//            }
//        } else {
//            return  SzResult.Error("管理员关闭了手机排队功能");
//        }
    }

    //过皮测试接口
    public SzResult weighTest(String truckNo) {
        QueryWrapper<CWeightSheets> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("WEISHT_TRUCKNO", truckNo).and(i -> i.ne("WEISHT_BACKED", 1).or().isNull("WEISHT_BACKED")).and(i -> i.ne("WEISHT_DELETED", 1).or().isNull("WEISHT_DELETED"));
        if (icWeightSheetsService.getOne(queryWrapper) != null) {
            return SzResult.error("已过皮");
        }
        QueryWrapper<CTrucks> trucksQueryWrapper = new QueryWrapper<>();
        trucksQueryWrapper.eq("TRUCKS_TRUCKNO", truckNo);
        trucksQueryWrapper.ne("TRUCKS_DELETED", 1);
        CTrucks truck = iCTrucksService.getOne(trucksQueryWrapper);
        truck.setTrucksQueueStatus(new BigDecimal(4));
        iCTrucksService.updateById(truck);

        CWeightSheets weightSheet = new CWeightSheets();
        String date = DateUtil.format(new Date(), "yyyyMMdd");
        QueryWrapper<CWeightSheets> weightSheetsQueryWrapper = new QueryWrapper<>();
        weightSheetsQueryWrapper.like("ID", date);
//        weightSheetsQueryWrapper.eq("DATA_FROM",)
        weightSheetsQueryWrapper.orderByDesc("ID");
        List<CWeightSheets> list = icWeightSheetsService.list(weightSheetsQueryWrapper);
        String dateId = list.size() == 0 ? null : list.get(0).getId();
        if (dateId == null) {
//            setFinanceEnterId是BigDecimal类型的，所以要转换
            weightSheet.setId(DateUtil.format(new Date(), "yyyyMMdd") + "001");
            weightSheet.setWeishtSheetno(DateUtil.format(new Date(), "yyyyMMdd") + "001");
        } else {
            //            substring是截取字符串，截取最后三位，也就是001
            String substring = dateId.substring(dateId.length() - 3);
//            substring1是截取前面的20230601
            String substring1 = dateId.substring(0, dateId.length() - 3);
//            substring2是截取的001转换成int类型的1
            int substring2 = Integer.parseInt(substring) + 1;
//            substring3是把1转换成字符串类型的001
            String substring3 = String.format("%03d", substring2);
//            substring4是把20230601和001拼接成20230601001
            String substring4 = substring1 + substring3;
//            substring5是把20230601001转换成BigDecimal类型的20230601001
//                BigDecimal substring5 = new BigDecimal();
            weightSheet.setId(substring4);
            weightSheet.setWeishtSheetno(substring4);
        }
        weightSheet.setWeishtIccardno(truck.getTrucksCheckinCardno());
        CCompanys company = iCCompanysService.getById(truck.getTrucksCheckinCo());
        if (company != null) {
            weightSheet.setWeishtCo(company.getCpyCo());
            weightSheet.setWeishtChiCo(company.getCpyChico());
        }
        CCompanys supplyCompany = iCCompanysService.getById(truck.getSupplyCoId());
        if (supplyCompany != null) {
            weightSheet.setWeishtCoSupply(supplyCompany.getCpyCo());
            weightSheet.setWeishtCoSupplyChi(supplyCompany.getCpyChico());
        }
        weightSheet.setWeishtTruckno(truck.getTrucksTruckno());
        weightSheet.setWeishtTrailerno(truck.getTrucksTrailerno());
        CProductsInfo productsInfo = iCProductsInfoService.getById(truck.getTrucksCheckinPdt());
        if (productsInfo != null) {
            weightSheet.setWeishtProduct(productsInfo.getPdtinfPdt());
        }

        weightSheet.setWeightSpecialPrice(truck.getSpecialPrice());
        QueryWrapper<CAshGateVideoBind> ashGateVideoBindQueryWrapper = new QueryWrapper<>();
        ashGateVideoBindQueryWrapper.eq("ASHGATE_NAME", truck.getTrucksCheckinLoadsite());
        ashGateVideoBindQueryWrapper.ne("ASHGATE_DELETED", 1);
        CAshGateVideoBind ashGateVideoBind = icAshGateVideoBindService.getOne(ashGateVideoBindQueryWrapper);
        if (ashGateVideoBind != null) {
            weightSheet.setWeishtAshgateId(ashGateVideoBind.getAshgateId());
        }
        weightSheet.setWeishtTare(new BigDecimal(1));
        weightSheet.setWeishtPriceId(truck.getPriceId());
        weightSheet.setWeishtPriceType(truck.getPriceType());
        weightSheet.setWeishtCheckinTm(truck.getTrucksCheckinTm());
        weightSheet.setWeishtCallinTm(truck.getTrucksCallinTm());
        weightSheet.setWeishtEmptyTm(LocalDateTime.now());
        weightSheet.setDataFrom(truck.getDataFrom());
        if (icWeightSheetsService.save(weightSheet)) {
            return SzResult.success("过磅成功");
        }
        return SzResult.error("过磅失败");
    }

    public SzResult returnWeighTest(String truckNo) {
        QueryWrapper<CTrucks> trucksQueryWrapper = new QueryWrapper<>();
        trucksQueryWrapper.eq("TRUCKS_TRUCKNO", truckNo);
        trucksQueryWrapper.ne("TRUCKS_DELETED", 1);
        CTrucks truck = iCTrucksService.getOne(trucksQueryWrapper);
        truck.setTrucksQueueStatus(new BigDecimal(0));
        iCTrucksService.updateById(truck);
        QueryWrapper<CWeightSheets> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("WEISHT_TRUCKNO", truckNo).and(i -> i.ne("WEISHT_BACKED", 1).or().isNull("WEISHT_BACKED")).and(i -> i.ne("WEISHT_DELETED", 1).or().isNull("WEISHT_DELETED"));
        CWeightSheets weightSheet = icWeightSheetsService.getOne(queryWrapper);
        weightSheet.setWeishtGrossWght(new BigDecimal(51));
        weightSheet.setWeishtNetWght(weightSheet.getWeishtGrossWght().subtract(weightSheet.getWeishtTare()));
        weightSheet.setWeishtBacked(true);
        weightSheet.setWeishtAshgateId(weightSheet.getWeishtAshgateId() == null ? new BigDecimal(1) : weightSheet.getWeishtAshgateId());
        QueryWrapper<CPdtsAuth> pdtsAuthQueryWrapper = new QueryWrapper<>();
        pdtsAuthQueryWrapper.eq("PDTSAUTH_PDT", weightSheet.getWeishtProduct());
        pdtsAuthQueryWrapper.eq("PDTSAUTH_CO", weightSheet.getWeishtCo());
        pdtsAuthQueryWrapper.eq("PDTSAUTH_CHICO", weightSheet.getWeishtChiCo());
        pdtsAuthQueryWrapper.ne("PDTSAUTH_DELETED", 1);
        CPdtsAuth pdtsAuth = iCPdtsAuthService.getOne(pdtsAuthQueryWrapper);
        if (pdtsAuth != null) {
            weightSheet.setWeishtMarketPrice(pdtsAuth.getPdtsauthPrice());
        }
        weightSheet.setWeishtFullTm(LocalDateTime.now());
        weightSheet.setWeishtOutTm(LocalDateTime.now());
        if (icWeightSheetsService.updateById(weightSheet)) {
            return SzResult.success("回磅成功");
        }
        return SzResult.error("回磅失败");
    }

    //查询所有等待未过磅的车
    public SzResult getAllWaitingTrucks(String token, String truckNo, String coId, String page, String pageSize, String plantId) {
        Page<CTrucks> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TRUCKS_QUEUE_STATUS", 1);
        queryWrapper.eq(coId != null && !coId.equals(""), "TRUCKS_CHECKIN_CO", coId);
        queryWrapper.ne("TRUCKS_DELETED", 1);
        queryWrapper.like(truckNo != null && !truckNo.equals(""), "TRUCKS_TRUCKNO", truckNo);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.orderByDesc("TRUCKS_QUEUE_STATUS");
        queryWrapper.orderByAsc("TRUCKS_CHECKIN_PDT", "TRUCKS_CHECKIN_LOADSITE", "TRUCKS_CHECKIN_NO");
//按照TRUCKS_CHECKIN_PDT分组
        //queryWrapper.groupBy("TRUCKS_CHECKIN_PDT");
//        新建一个list
        IPage<CTrucks> iPage = iCTrucksService.page(page1, queryWrapper);
        List<CTrucks> list = iPage.getRecords();
        long total = iPage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        List<TrucksVo> trucksVos = new ArrayList<>();
        for (CTrucks cTrucks : list) {
            TrucksVo trucksVo = new TrucksVo();
            trucksVo.setId(cTrucks.getId());
            trucksVo.setTrucksCheckinLoadsiteSn(cTrucks.getTrucksCheckinLoadsiteSn());
            trucksVo.setPriceId(cTrucks.getPriceId());
            trucksVo.setPriceType(cTrucks.getPriceType());
            trucksVo.setSupplyCoId(cTrucks.getSupplyCoId());
            if (cTrucks.getSupplyCoId() != null) {
                CCompanys cCompanys = iCCompanysService.getById(cTrucks.getSupplyCoId());
                if (cCompanys != null) {
                    trucksVo.setSupplyCoName(cCompanys.getCpyCo() + "." + cCompanys.getCpyChico());
                }
            }
            trucksVo.setSpecialPrice(cTrucks.getSpecialPrice());
            trucksVo.setTrucksId(String.valueOf(cTrucks.getTrucksId()));
            trucksVo.setTrucksSn(cTrucks.getTrucksSn());
            trucksVo.setTrucksTruckno(cTrucks.getTrucksTruckno());
            trucksVo.setTrucksTrailerno(cTrucks.getTrucksTrailerno());
            trucksVo.setTrucksType(cTrucks.getTrucksType());
            trucksVo.setTrucksPriv(cTrucks.getTrucksPriv());
            trucksVo.setTrucksEmiss(cTrucks.getTrucksEmiss());
            trucksVo.setTrucksAxles(cTrucks.getTrucksAxles());
            trucksVo.setTrucksLength(cTrucks.getTrucksLength());
            trucksVo.setTrucksMaster(cTrucks.getTrucksMaster());
            trucksVo.setTrucksIdentity(cTrucks.getTrucksIdentity());
            trucksVo.setTrucksTell(cTrucks.getTrucksTell());
            trucksVo.setTrucksTag1(cTrucks.getTrucksTag1());
            trucksVo.setTrucksTag2(cTrucks.getTrucksTag2());
            trucksVo.setTrucksTag3(cTrucks.getTrucksTag3());
            trucksVo.setTrucksRefTare(cTrucks.getTrucksRefTare());
            trucksVo.setTrucksRefNet(cTrucks.getTrucksRefNet());
            trucksVo.setTrucksAgainMinTm(cTrucks.getTrucksAgainMinTm());
            trucksVo.setTrucksDispatchPdt(cTrucks.getTrucksDispatchPdt());
            trucksVo.setTrucksDispatchCo(cTrucks.getTrucksDispatchCo());
            trucksVo.setTrucksDispatchTm(cTrucks.getTrucksDispatchTm());
            trucksVo.setTrucksDispatcher(cTrucks.getTrucksDispatcher());
            trucksVo.setTrucksRemark(cTrucks.getTrucksRemark());
            trucksVo.setTrucksCheckinNo(cTrucks.getTrucksCheckinNo());
            trucksVo.setTrucksCheckinCardno(cTrucks.getTrucksCheckinCardno());
            trucksVo.setTrucksCheckinCo(cTrucks.getTrucksCheckinCo());
            if (cTrucks.getTrucksCheckinCo() == null || cTrucks.getTrucksCheckinCo().equals("") ) {
                trucksVo.setTrucksCheckinCoName("");

                trucksVo.setTrucksCheckinChiCoName("");
                //
            } else {
                QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("ID", cTrucks.getTrucksCheckinCo());
                queryWrapper1.eq("CPY_DELETED", 0);
                CCompanys cCompanys = iCCompanysService.getOne(queryWrapper1);
                if (cCompanys == null) {
                    trucksVo.setTrucksCheckinCoName("");
                    trucksVo.setTrucksCheckinChiCoName("");
                } else {
                    trucksVo.setTrucksCheckinCoName(cCompanys.getCpyCo());
                    trucksVo.setTrucksCheckinChiCoName(cCompanys.getCpyChico());
                }
            }
            trucksVo.setTrucksCheckinTag(cTrucks.getTrucksCheckinTag());
            trucksVo.setTrucksCheckinPdt(cTrucks.getTrucksCheckinPdt());
            if (cTrucks.getTrucksCheckinPdt() == null || cTrucks.getTrucksCheckinPdt().equals("")) {
                trucksVo.setTrucksCheckinPdtName("");
            } else {
                QueryWrapper<CProductsInfo> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("ID", cTrucks.getTrucksCheckinPdt());
                queryWrapper2.eq("PDTINF_DELETED", 0);
                CProductsInfo cProductsInfo = iCProductsInfoService.getOne(queryWrapper2);
                if (cProductsInfo == null)
                    trucksVo.setTrucksCheckinPdtName("");
                else
                    trucksVo.setTrucksCheckinPdtName(cProductsInfo.getPdtinfPdt());
            }
            trucksVo.setTrucksCheckinPdtsn(cTrucks.getTrucksCheckinPdtsn());
            trucksVo.setTrucksCheckinCoTran(cTrucks.getTrucksCheckinCoTran());
            if (cTrucks.getTrucksCheckinCoTran() == null || !cTrucks.getTrucksCheckinCoTran().equals("")) {
                trucksVo.setTrucksCheckinCoTranName("");
            } else {
                QueryWrapper<CCompanys> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("ID", cTrucks.getTrucksCheckinCoTran());
                queryWrapper3.eq("CPY_DELETED", 0);
                CCompanys cCompanys1 = iCCompanysService.getOne(queryWrapper3);
                if (cCompanys1 == null) {
                    trucksVo.setTrucksCheckinCoTranName("");
                } else {
                    trucksVo.setTrucksCheckinCoTranName(cCompanys1.getCpyCo());
                }
            }
            if (StringUtils.isNotEmpty(cTrucks.getBindCoId())) {
                List<Map<String, Object>> coList = new ArrayList<>();
                for (String id : Arrays.asList(cTrucks.getBindCoId().split(","))) {
                    QueryWrapper<CCompanys> queryWrapper4 = new QueryWrapper<>();
                    queryWrapper4.eq("ID", id);
                    queryWrapper4.eq("CPY_DELETED", 0);
                    queryWrapper4.eq("DATA_FROM", plantId);

                    for (CCompanys cCompanys : iCCompanysService.list(queryWrapper4)) {
                        Map<String, Object> resMap = new HashMap<>();
                        resMap.put("coName", cCompanys.getCpyCo() + cCompanys.getCpyChico());
                        resMap.put("id", cCompanys.getId());
                        coList.add(resMap);
                    }
                }
                trucksVo.setCoList(coList);
            }
            trucksVo.setTrucksCheckinBatchSn(cTrucks.getTrucksCheckinBatchSn());
            trucksVo.setTrucksCheckinBatch(cTrucks.getTrucksCheckinBatch());
            trucksVo.setTrucksCheckinLoadsite(cTrucks.getTrucksCheckinLoadsite());
//            trucksPreDeduct
            trucksVo.setTrucksPreDeduct(cTrucks.getTrucksPreDeduct());
//            trucksBillGross
            trucksVo.setTrucksBillGross(cTrucks.getTrucksBillGross());
//trucksBillTare
            trucksVo.setTrucksBillTare(cTrucks.getTrucksBillTare());
            //trucksBillNet
            trucksVo.setTrucksBillNet(cTrucks.getTrucksBillNet());
            //trucksDepStation
            trucksVo.setTrucksDepStation(cTrucks.getTrucksDepStation());
            //trucksBoat
            trucksVo.setTrucksBoat(cTrucks.getTrucksBoat());
            //trucksShip
            trucksVo.setTrucksShip(cTrucks.getTrucksShip());
            //trucksMineral
            trucksVo.setTrucksMineral(cTrucks.getTrucksMineral());
            //trucksCoaltype
            trucksVo.setTrucksCoaltype(cTrucks.getTrucksCoaltype());
//            trucksShipTm
            trucksVo.setTrucksShipTm(cTrucks.getTrucksShipTm());
            //trucksDelyTm
            trucksVo.setTrucksDelyTm(cTrucks.getTrucksDelyTm());
            //trucksCheckinTm
            trucksVo.setTrucksCheckinTm(cTrucks.getTrucksCheckinTm());
//            trucksCallinTm

            trucksVo.setTrucksCallinTm(cTrucks.getTrucksCallinTm());
//            trucksEnterTm
            trucksVo.setTrucksEnterTm(cTrucks.getTrucksEnterTm());
            //trucksPoundinTm
            trucksVo.setTrucksPoundinTm(cTrucks.getTrucksPoundinTm());
            //trucksPoundoutTm
            trucksVo.setTrucksPoundoutTm(cTrucks.getTrucksPoundoutTm());
            //trucksCheckinOperator
            trucksVo.setTrucksCheckinOperator(cTrucks.getTrucksCheckinOperator());
            //trucksRegMan
            trucksVo.setTrucksRegMan(cTrucks.getTrucksRegMan());
            //trucksFixMan
            trucksVo.setTrucksFixMan(cTrucks.getTrucksFixMan());
            //trucksFixTm
            trucksVo.setTrucksFixTm(cTrucks.getTrucksFixTm());
            //trucksQueuePreStatus
            trucksVo.setTrucksQueuePreStatus(cTrucks.getTrucksQueuePreStatus());
            //trucksQueueStatus
            trucksVo.setTrucksQueueStatus(cTrucks.getTrucksQueueStatus());
            //trucksSampling
            trucksVo.setTrucksSampling(cTrucks.getTrucksSampling());
            //svrTmstamp
            trucksVo.setSvrTmstamp(cTrucks.getSvrTmstamp());
            //cliTmstamp
            trucksVo.setCliTmstamp(cTrucks.getCliTmstamp());
            //trucksCheckinIdentied
            trucksVo.setTrucksCheckinIdentied(cTrucks.getTrucksCheckinIdentied());
            //trucksEntered
            trucksVo.setTrucksEntered(cTrucks.getTrucksEntered());
            //trucksBreakdown
            trucksVo.setTrucksBreakdown(cTrucks.getTrucksBreakdown());
            //trucksLinger
            trucksVo.setTrucksLinger(cTrucks.getTrucksLinger());
            //trucksRefTareInited
            trucksVo.setTrucksRefTareInited(cTrucks.getTrucksRefTareInited());
            //trucksMustQueue
            trucksVo.setTrucksMustQueue(cTrucks.getTrucksMustQueue());
            //trucksIdentyQueue
            trucksVo.setTrucksIdentyQueue(cTrucks.getTrucksIdentyQueue());
            //trucksIdentyLoad
            trucksVo.setTrucksIdentyLoad(cTrucks.getTrucksIdentyLoad());
            //trucksInfoApproved
            trucksVo.setTrucksInfoApproved(cTrucks.getTrucksInfoApproved());
            //trucksDeleted
            trucksVo.setTrucksDeleted(cTrucks.getTrucksDeleted());
            //trucksNew
            trucksVo.setTrucksNew(cTrucks.getTrucksNew());
//            downedID1
            trucksVo.setDownedID1(cTrucks.getDownedID1());
//            downedID2
            trucksVo.setDownedID2(cTrucks.getDownedID2());
//            downedID3
            trucksVo.setDownedID3(cTrucks.getDownedID3());
//            downedID4
            trucksVo.setDownedID4(cTrucks.getDownedID4());
//            downedID5
            trucksVo.setDownedID5(cTrucks.getDownedID5());
//            downedID6
            trucksVo.setDownedID6(cTrucks.getDownedID6());
//            downedID7
            trucksVo.setDownedID7(cTrucks.getDownedID7());
//            downedID8
            trucksVo.setDownedID8(cTrucks.getDownedID8());
//            downedID9
            trucksVo.setDownedID9(cTrucks.getDownedID9());
//            downedID10
            trucksVo.setDownedID10(cTrucks.getDownedID10());
//            downedID11
            trucksVo.setDownedID11(cTrucks.getDownedID11());
//            downedID12
            trucksVo.setDownedID12(cTrucks.getDownedID12());
//            downedID13
            trucksVo.setDownedID13(cTrucks.getDownedID13());
//            downedID14
            trucksVo.setDownedID14(cTrucks.getDownedID14());
//            downedID15
            trucksVo.setDownedID15(cTrucks.getDownedID15());
//            downedID16
            trucksVo.setDownedID16(cTrucks.getDownedID16());
//            downedID17
            trucksVo.setDownedID17(cTrucks.getDownedID17());
//            downedID18
            trucksVo.setDownedID18(cTrucks.getDownedID18());
//            downedID19
            trucksVo.setDownedID19(cTrucks.getDownedID19());
//            downedID20
            trucksVo.setDownedID20(cTrucks.getDownedID20());
//            dataFrom
            trucksVo.setDataFrom(cTrucks.getDataFrom());
//            serialNo
            trucksVo.setSerialNo(cTrucks.getSerialNo());

            //            如果车辆是等待状态就不显示装载点等信息
            if (cTrucks.getTrucksQueueStatus().equals(BigDecimal.ONE)) {
                trucksVo.setTrucksCallinTm(null);
                trucksVo.setTrucksCheckinLoadsite(null);
                trucksVo.setSpecialPrice(null);
            }
            trucksVos.add(trucksVo);
        }
        map.put("list", trucksVos);
        return SzResult.success(map);
    }

    //查询所有已经叫号的车
    public SzResult getAllCallingTrucks(String token, String truckNo, String coId, String page, String pageSize, String plantId) {
        Page<CTrucks> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TRUCKS_QUEUE_STATUS", 2);
        queryWrapper.eq(coId != null && !coId.equals(""), "TRUCKS_CHECKIN_CO", coId);
        queryWrapper.ne("TRUCKS_DELETED", 1);
        queryWrapper.like(truckNo != null && !truckNo.equals(""), "TRUCKS_TRUCKNO", truckNo);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.orderByDesc("TRUCKS_QUEUE_STATUS");
        queryWrapper.orderByAsc("TRUCKS_CHECKIN_PDT", "TRUCKS_CHECKIN_LOADSITE", "TRUCKS_CHECKIN_NO");
//按照TRUCKS_CHECKIN_PDT分组
        //queryWrapper.groupBy("TRUCKS_CHECKIN_PDT");
//        新建一个list
        IPage<CTrucks> iPage = iCTrucksService.page(page1, queryWrapper);
        List<CTrucks> list = iPage.getRecords();
        long total = iPage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        List<TrucksVo> trucksVos = new ArrayList<>();
        for (CTrucks cTrucks : list) {
            TrucksVo trucksVo = new TrucksVo();
            trucksVo.setTrucksCheckinLoadsiteSn(cTrucks.getTrucksCheckinLoadsiteSn());
            trucksVo.setPriceId(cTrucks.getPriceId());
            trucksVo.setPriceType(cTrucks.getPriceType());
            trucksVo.setSupplyCoId(cTrucks.getSupplyCoId());
            if (cTrucks.getSupplyCoId() != null) {
                CCompanys cCompanys = iCCompanysService.getById(cTrucks.getSupplyCoId());
                if (cCompanys != null) {
                    trucksVo.setSupplyCoName(cCompanys.getCpyCo() + "." + cCompanys.getCpyChico());
                }
            }
            trucksVo.setSpecialPrice(cTrucks.getSpecialPrice());
            trucksVo.setTrucksId(String.valueOf(cTrucks.getTrucksId()));
            trucksVo.setTrucksSn(cTrucks.getTrucksSn());
            trucksVo.setTrucksTruckno(cTrucks.getTrucksTruckno());
            trucksVo.setTrucksTrailerno(cTrucks.getTrucksTrailerno());
            trucksVo.setTrucksType(cTrucks.getTrucksType());
            trucksVo.setTrucksPriv(cTrucks.getTrucksPriv());
            trucksVo.setTrucksEmiss(cTrucks.getTrucksEmiss());
            trucksVo.setTrucksAxles(cTrucks.getTrucksAxles());
            trucksVo.setTrucksLength(cTrucks.getTrucksLength());
            trucksVo.setTrucksMaster(cTrucks.getTrucksMaster());
            trucksVo.setTrucksIdentity(cTrucks.getTrucksIdentity());
            trucksVo.setTrucksTell(cTrucks.getTrucksTell());
            trucksVo.setTrucksTag1(cTrucks.getTrucksTag1());
            trucksVo.setTrucksTag2(cTrucks.getTrucksTag2());
            trucksVo.setTrucksTag3(cTrucks.getTrucksTag3());
            trucksVo.setTrucksRefTare(cTrucks.getTrucksRefTare());
            trucksVo.setTrucksRefNet(cTrucks.getTrucksRefNet());
            trucksVo.setTrucksAgainMinTm(cTrucks.getTrucksAgainMinTm());
            trucksVo.setTrucksDispatchPdt(cTrucks.getTrucksDispatchPdt());
            trucksVo.setTrucksDispatchCo(cTrucks.getTrucksDispatchCo());
            trucksVo.setTrucksDispatchTm(cTrucks.getTrucksDispatchTm());
            trucksVo.setTrucksDispatcher(cTrucks.getTrucksDispatcher());
            trucksVo.setTrucksRemark(cTrucks.getTrucksRemark());
            trucksVo.setTrucksCheckinNo(cTrucks.getTrucksCheckinNo());
            trucksVo.setTrucksCheckinCardno(cTrucks.getTrucksCheckinCardno());
            trucksVo.setTrucksCheckinCo(cTrucks.getTrucksCheckinCo());
            if (cTrucks.getTrucksCheckinCo() == null || cTrucks.getTrucksCheckinCo().equals("") ) {
                trucksVo.setTrucksCheckinCoName("");
                trucksVo.setTrucksCheckinChiCoName("");
                //
            } else {
                QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("ID", cTrucks.getTrucksCheckinCo());
                queryWrapper1.eq("CPY_DELETED", 0);
                CCompanys cCompanys = iCCompanysService.getOne(queryWrapper1);
                if (cCompanys == null) {
                    trucksVo.setTrucksCheckinCoName("");
                    trucksVo.setTrucksCheckinChiCoName("");
                } else {
                    trucksVo.setTrucksCheckinCoName(cCompanys.getCpyCo());
                    trucksVo.setTrucksCheckinChiCoName(cCompanys.getCpyChico());
                }
            }
            trucksVo.setTrucksCheckinTag(cTrucks.getTrucksCheckinTag());
            trucksVo.setTrucksCheckinPdt(cTrucks.getTrucksCheckinPdt());
            if (cTrucks.getTrucksCheckinPdt() == null || cTrucks.getTrucksCheckinPdt().equals("")) {
                trucksVo.setTrucksCheckinPdtName("");
            } else {
                QueryWrapper<CProductsInfo> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("ID", cTrucks.getTrucksCheckinPdt());
                queryWrapper2.eq("PDTINF_DELETED", 0);
                CProductsInfo cProductsInfo = iCProductsInfoService.getOne(queryWrapper2);
                if (cProductsInfo == null)
                    trucksVo.setTrucksCheckinPdtName("");
                else
                    trucksVo.setTrucksCheckinPdtName(cProductsInfo.getPdtinfPdt());
            }
            trucksVo.setTrucksCheckinPdtsn(cTrucks.getTrucksCheckinPdtsn());

            trucksVo.setTrucksCheckinCoTran(cTrucks.getTrucksCheckinCoTran());
            if (cTrucks.getTrucksCheckinCoTran() == null || cTrucks.getTrucksCheckinCoTran().equals("") ) {
                trucksVo.setTrucksCheckinCoTranName("");

            } else {
                QueryWrapper<CCompanys> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("ID", cTrucks.getTrucksCheckinCoTran());
                queryWrapper3.eq("CPY_DELETED", 0);
                CCompanys cCompanys1 = iCCompanysService.getOne(queryWrapper3);
                if (cCompanys1 == null) {
                    trucksVo.setTrucksCheckinCoTranName("");
                } else {
                    trucksVo.setTrucksCheckinCoTranName(cCompanys1.getCpyCo());
                }
            }
            if (StringUtils.isNotEmpty(cTrucks.getBindCoId())) {
                List<Map<String, Object>> coList = new ArrayList<>();
                for (String id : Arrays.asList(cTrucks.getBindCoId().split(","))) {
                    QueryWrapper<CCompanys> queryWrapper4 = new QueryWrapper<>();
                    queryWrapper4.eq("ID", id);
                    queryWrapper4.eq("CPY_DELETED", 0);
                    queryWrapper4.eq("DATA_FROM", plantId);

                    for (CCompanys cCompanys : iCCompanysService.list(queryWrapper4)) {
                        Map<String, Object> resMap = new HashMap<>();
                        resMap.put("coName", cCompanys.getCpyCo() + cCompanys.getCpyChico());
                        resMap.put("id", cCompanys.getId());
                        coList.add(resMap);
                    }
                }
                trucksVo.setCoList(coList);
            }
            trucksVo.setTrucksCheckinBatchSn(cTrucks.getTrucksCheckinBatchSn());
            trucksVo.setTrucksCheckinBatch(cTrucks.getTrucksCheckinBatch());
            trucksVo.setTrucksCheckinLoadsite(cTrucks.getTrucksCheckinLoadsite());
//            trucksPreDeduct
            trucksVo.setTrucksPreDeduct(cTrucks.getTrucksPreDeduct());
//            trucksBillGross
            trucksVo.setTrucksBillGross(cTrucks.getTrucksBillGross());
//trucksBillTare
            trucksVo.setTrucksBillTare(cTrucks.getTrucksBillTare());
            //trucksBillNet
            trucksVo.setTrucksBillNet(cTrucks.getTrucksBillNet());
            //trucksDepStation
            trucksVo.setTrucksDepStation(cTrucks.getTrucksDepStation());
            //trucksBoat
            trucksVo.setTrucksBoat(cTrucks.getTrucksBoat());
            //trucksShip
            trucksVo.setTrucksShip(cTrucks.getTrucksShip());
            //trucksMineral
            trucksVo.setTrucksMineral(cTrucks.getTrucksMineral());
            //trucksCoaltype
            trucksVo.setTrucksCoaltype(cTrucks.getTrucksCoaltype());
//            trucksShipTm
            trucksVo.setTrucksShipTm(cTrucks.getTrucksShipTm());
            //trucksDelyTm
            trucksVo.setTrucksDelyTm(cTrucks.getTrucksDelyTm());
            //trucksCheckinTm
            trucksVo.setTrucksCheckinTm(cTrucks.getTrucksCheckinTm());
//            trucksCallinTm

            trucksVo.setTrucksCallinTm(cTrucks.getTrucksCallinTm());
//            trucksEnterTm
            trucksVo.setTrucksEnterTm(cTrucks.getTrucksEnterTm());
            //trucksPoundinTm
            trucksVo.setTrucksPoundinTm(cTrucks.getTrucksPoundinTm());
            //trucksPoundoutTm
            trucksVo.setTrucksPoundoutTm(cTrucks.getTrucksPoundoutTm());
            //trucksCheckinOperator
            trucksVo.setTrucksCheckinOperator(cTrucks.getTrucksCheckinOperator());
            //trucksRegMan
            trucksVo.setTrucksRegMan(cTrucks.getTrucksRegMan());
            //trucksFixMan
            trucksVo.setTrucksFixMan(cTrucks.getTrucksFixMan());
            //trucksFixTm
            trucksVo.setTrucksFixTm(cTrucks.getTrucksFixTm());
            //trucksQueuePreStatus
            trucksVo.setTrucksQueuePreStatus(cTrucks.getTrucksQueuePreStatus());
            //trucksQueueStatus
            trucksVo.setTrucksQueueStatus(cTrucks.getTrucksQueueStatus());
            //trucksSampling
            trucksVo.setTrucksSampling(cTrucks.getTrucksSampling());
            //svrTmstamp
            trucksVo.setSvrTmstamp(cTrucks.getSvrTmstamp());
            //cliTmstamp
            trucksVo.setCliTmstamp(cTrucks.getCliTmstamp());
            //trucksCheckinIdentied
            trucksVo.setTrucksCheckinIdentied(cTrucks.getTrucksCheckinIdentied());
            //trucksEntered
            trucksVo.setTrucksEntered(cTrucks.getTrucksEntered());
            //trucksBreakdown
            trucksVo.setTrucksBreakdown(cTrucks.getTrucksBreakdown());
            //trucksLinger
            trucksVo.setTrucksLinger(cTrucks.getTrucksLinger());
            //trucksRefTareInited
            trucksVo.setTrucksRefTareInited(cTrucks.getTrucksRefTareInited());
            //trucksMustQueue
            trucksVo.setTrucksMustQueue(cTrucks.getTrucksMustQueue());
            //trucksIdentyQueue
            trucksVo.setTrucksIdentyQueue(cTrucks.getTrucksIdentyQueue());
            //trucksIdentyLoad
            trucksVo.setTrucksIdentyLoad(cTrucks.getTrucksIdentyLoad());
            //trucksInfoApproved
            trucksVo.setTrucksInfoApproved(cTrucks.getTrucksInfoApproved());
            //trucksDeleted
            trucksVo.setTrucksDeleted(cTrucks.getTrucksDeleted());
            //trucksNew
            trucksVo.setTrucksNew(cTrucks.getTrucksNew());
//            downedID1
            trucksVo.setDownedID1(cTrucks.getDownedID1());
//            downedID2
            trucksVo.setDownedID2(cTrucks.getDownedID2());
//            downedID3
            trucksVo.setDownedID3(cTrucks.getDownedID3());
//            downedID4
            trucksVo.setDownedID4(cTrucks.getDownedID4());
//            downedID5
            trucksVo.setDownedID5(cTrucks.getDownedID5());
//            downedID6
            trucksVo.setDownedID6(cTrucks.getDownedID6());
//            downedID7
            trucksVo.setDownedID7(cTrucks.getDownedID7());
//            downedID8
            trucksVo.setDownedID8(cTrucks.getDownedID8());
//            downedID9
            trucksVo.setDownedID9(cTrucks.getDownedID9());
//            downedID10
            trucksVo.setDownedID10(cTrucks.getDownedID10());
//            downedID11
            trucksVo.setDownedID11(cTrucks.getDownedID11());
//            downedID12
            trucksVo.setDownedID12(cTrucks.getDownedID12());
//            downedID13
            trucksVo.setDownedID13(cTrucks.getDownedID13());
//            downedID14
            trucksVo.setDownedID14(cTrucks.getDownedID14());
//            downedID15
            trucksVo.setDownedID15(cTrucks.getDownedID15());
//            downedID16
            trucksVo.setDownedID16(cTrucks.getDownedID16());
//            downedID17
            trucksVo.setDownedID17(cTrucks.getDownedID17());
//            downedID18
            trucksVo.setDownedID18(cTrucks.getDownedID18());
//            downedID19
            trucksVo.setDownedID19(cTrucks.getDownedID19());
//            downedID20
            trucksVo.setDownedID20(cTrucks.getDownedID20());
//            dataFrom
            trucksVo.setDataFrom(cTrucks.getDataFrom());
//            serialNo
            trucksVo.setSerialNo(cTrucks.getSerialNo());

            //            如果车辆是等待状态就不显示装载点等信息
            if (cTrucks.getTrucksQueueStatus().equals(BigDecimal.ONE)) {
                trucksVo.setTrucksCallinTm(null);
                trucksVo.setTrucksCheckinLoadsite(null);
                trucksVo.setSpecialPrice(null);
            }
            trucksVos.add(trucksVo);
        }
        map.put("list", trucksVos);
        return SzResult.success(map);
    }

    //    取消排队truckQueueBu.cancelQueue(id, token, plantId);
    public SzResult cancelQueue(String id,  String plantId) {
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = iCTrucksService.getOne(queryWrapper);
        if (cTrucks == null) {
            return SzResult.success("未找到该车辆");
        }
        if (cTrucks.getTrucksQueueStatus().compareTo(BigDecimal.valueOf(1)) > 0) {
            return SzResult.success("该车辆不在等待状态，无法取消排队");
        }
        cTrucks.setTrucksQueueStatus(BigDecimal.ZERO);
        cTrucks.setTrucksQueueStatus(BigDecimal.valueOf(0));
        cTrucks.setTrucksQueueVip(0);
        cTrucks.setTrucksCheckinNo(null);
        cTrucks.setTrucksCallinTm(null);
        cTrucks.setTrucksCheckinTm(null);
        cTrucks.setTrucksCheckinPdt("");
        cTrucks.setTrucksCheckinCo("");
        cTrucks.setTrucksCheckinLoadsite("");
        cTrucks.setTrucksCheckinLoadsiteSn(null);
        cTrucks.setTrucksCheckinCardno("");
        cTrucks.setTrucksCheckinLoadsiteAdd("");
        cTrucks.setSupplyCoId("");
        boolean b = iCTrucksService.updateById(cTrucks);
        if (b) {
            QueryWrapper<CTrucksUpdate> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("TRUCKS_ID", cTrucks.getTrucksId());
            queryWrapper3.eq("DATA_FROM", plantId);
            queryWrapper3.eq("HANDLED_RESULT", 0);
            List<CTrucksUpdate> list = iCTrucksUpdateService.list(queryWrapper3);
            for (CTrucksUpdate cTrucksUpdate : list) {
                cTrucksUpdate.setHandledResult(100);
                cTrucksUpdate.setHandled(String.valueOf(22222222));
                cTrucksUpdate.setHandledResultStr("回复：取消排队");
                iCTrucksUpdateService.updateById(cTrucksUpdate);
            }
            return SzResult.success("取消成功");
        }
        else {
            return SzResult.success("取消失败");
        }
    }
    //    本地取消排队truckQueueBu.cancelQueue(id, token, plantId);
    public SzResult cancelTruckQueue(String id,  String plantId) {
        Map<String, Object> map1 = new HashMap<>();
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = iCTrucksService.getOne(queryWrapper);
        if (cTrucks == null) {
            map1.put("code", 1);
            map1.put("msg", "未找到该车辆");
            return SzResult.success(map1);
        }
        if (cTrucks.getTrucksQueueStatus().compareTo(BigDecimal.valueOf(1)) > 0) {
            map1.put("code", 1);
            map1.put("msg", "该车辆不在等待状态，无法取消排队");
            return SzResult.success(map1);
        }
        cTrucks.setTrucksQueueStatus(BigDecimal.ZERO);
        cTrucks.setTrucksQueueVip(0);
        cTrucks.setTrucksCheckinNo(null);
        cTrucks.setTrucksCallinTm(null);
        cTrucks.setTrucksCheckinTm(null);
        cTrucks.setTrucksCheckinPdt("");
        cTrucks.setTrucksCheckinCo("");
        cTrucks.setTrucksCheckinLoadsite("");
        cTrucks.setTrucksCheckinLoadsiteSn(null);
        cTrucks.setTrucksCheckinCardno("");
        cTrucks.setTrucksCheckinLoadsiteAdd("");
        cTrucks.setSupplyCoId("");
        boolean b = iCTrucksService.updateById(cTrucks);
        if (b) {
            QueryWrapper<CTrucksUpdate> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("TRUCKS_ID", cTrucks.getTrucksId());
            queryWrapper3.eq("DATA_FROM", plantId);
            queryWrapper3.eq("HANDLED_RESULT", "0");
            List<CTrucksUpdate> list = iCTrucksUpdateService.list(queryWrapper3);
            for (CTrucksUpdate cTrucksUpdate : list) {
                cTrucksUpdate.setHandledResult(100);
                cTrucksUpdate.setHandled(String.valueOf(22222222));
                cTrucksUpdate.setHandledResultStr("回复：取消排队");
                iCTrucksUpdateService.updateById(cTrucksUpdate);
            }
            map1.put("code", 0);
            map1.put("msg", "取消成功");
            return SzResult.success(map1);
        }
        else {
            map1.put("code", 1);
            map1.put("msg", "取消失败");
            return SzResult.success(map1);
        }
    }

    //    手动叫号


    //==================手动执行叫号逻辑============================
    public SzResult manualCallin(String id, String ashGateId, String token, String plantId) {
        QueryWrapper<CAshGateVideoBind> gateVideoBindWrapper = new QueryWrapper<>();
        gateVideoBindWrapper.eq("ID", ashGateId);
        gateVideoBindWrapper.eq("DATA_FROM", plantId);
        gateVideoBindWrapper.eq("ASHGATE_DELETED", 0);
        CAshGateVideoBind cAshGateVideoBind = iCAshGateVideoBindService.getOne(gateVideoBindWrapper);
        LocalDateTime callInTM = LocalDateTime.now();
        QueryWrapper<CTrucks> cTrucksQueryWrapper = new QueryWrapper<>();
        cTrucksQueryWrapper.eq("ID", id);
//        查询正在等待的车
        cTrucksQueryWrapper.eq("TRUCKS_QUEUE_STATUS", 1);
        cTrucksQueryWrapper.eq("TRUCKS_DELETED", 0);
        CTrucks cTrucks = iCTrucksService.getOne(cTrucksQueryWrapper);
//       循环所有没叫号的车
        if (cTrucks == null){
            return SzResult.error("此车已叫号，或不存在");
        }

//            拿到车辆排队的品种
        String pdtId = cTrucks.getTrucksCheckinPdt();
//            根据品种查出装载点
        QueryWrapper<CProductsInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("ID", pdtId);
        wrapper.ne("PDTINF_DELETED", 1);
        CProductsInfo cProductsInfo = iCProductsInfoService.getOne(wrapper);
        QueryWrapper<CCompanys> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("ID", cTrucks.getTrucksCheckinCo());
        CCompanys cCompanys = iCCompanysService.getOne(wrapper1);
        QueryWrapper<CPdtClass> pdtClasswrapper = new QueryWrapper<>();
        pdtClasswrapper.eq("ID", cProductsInfo.getPdtinfClass());
        CPdtClass cPdtClass = iCPdtClassService.getOne(pdtClasswrapper);
//            这里判断一下是否在石膏叫号的时间范围内，在石膏叫号的时间范围内就给石膏叫号
        if (cPdtClass.getId().equals("2")) {
            QueryWrapper<SysDic> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("DIC_KEY", "truck_call_start_shigao");
            queryWrapper.eq("DATA_FROM", plantId);
            SysDic sysDic = sysDicService.getOne(queryWrapper);
            QueryWrapper<SysDic> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("DIC_KEY", "truck_call_stop_shigao");
            queryWrapper1.eq("DATA_FROM", plantId);
            SysDic sysDic1 = sysDicService.getOne(queryWrapper1);
            if (sysDic != null && sysDic1 != null) {
                String startTimeStr = sysDic.getDicVal();
                String endTimeStr = sysDic1.getDicVal();
                LocalTime startTime = LocalTime.parse(startTimeStr, DateTimeFormatter.ofPattern("HH:mm"));
                LocalTime endTime = LocalTime.parse(endTimeStr, DateTimeFormatter.ofPattern("HH:mm"));
                LocalTime now = LocalTime.now();
                if (now.isBefore(startTime) || now.isAfter(endTime)) {
                    return SzResult.error("不在石膏叫号的时间范围内");
                }
            }
        }
//            这里拦截一下，某些品种排队了直接就叫号，发货单位(收款单位)根据计划里的拿到
//            这里只判断计划，不叫号
        if (
//                    cPdtClass.getId().equals("a45ed1623ea342cc8544f8db17eff1fb") ||
//                    石膏品类
                cPdtClass.getId().equals("2") ||
                        cProductsInfo.getId().equals("c9cf42874ddc446891ec936c5bf67386")//  储备库炉渣
        ) {
//                检查储备库炉渣是否强制开启计划判断
            QueryWrapper<SysDic> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("DIC_KEY", "queueControl_chubkuluzha_plan");
            queryWrapper1.eq("DATA_FROM", plantId);
            SysDic sysDic = sysDicService.getOne(queryWrapper1);
            if (sysDic.getDicVal().equals("1")) {
                QueryWrapper<TGypsumPlan> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("CO_ID", cCompanys.getId());
                queryWrapper.eq("PDT_ID", pdtId);
                queryWrapper.eq("PLAN_TIME", cTrucks.getTrucksCheckinTm().truncatedTo(ChronoUnit.DAYS));
                TGypsumPlan gypsumPlan = iTGypsumPlanService.getOne(queryWrapper);
                if (gypsumPlan != null) {
                    CTrucks cTrucks2 = new CTrucks();
                    cTrucks2.setTrucksCallinTm(callInTM);
//                这里根据计划拿到了收款单位
                    cTrucks2.setSupplyCoId(gypsumPlan.getSupplyCoId());
                    UpdateWrapper<CTrucks> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("ID", cTrucks.getId());
                    iCTrucksService.update(cTrucks2, updateWrapper);
                } else {
                    return SzResult.error("没有找到对应的计划");
                }
            }
        }
        //                如果是大袋灰、一二期石膏、三四期石膏、石膏就写死装载点
        if (cProductsInfo.getId().equals("56260577b0ab4b5d9b84d9577fcac1d0")//大袋灰
                || cProductsInfo.getId().equals("1dce738280ff4cfc946659990c8a05b6")//钢板仓粗灰
                || cProductsInfo.getId().equals("025c559037d7462383b053630239ee49")//钢板仓原灰
                || cProductsInfo.getId().equals("8bebefb14fe3419ea957591b01774274")//一二期石膏
                || cProductsInfo.getId().equals("3365a55ec72e4e12b1ace84b2fdd9bec")//三四期石膏
                || cProductsInfo.getId().equals("7472d52542c54ce78917781471497559")//石膏
                || cProductsInfo.getId().equals("c9cf42874ddc446891ec936c5bf67386")//储备库炉渣
                || cProductsInfo.getId().equals("af09ebfbecc04a4487f391f38567d8a8")//#6炉炉渣
                || cProductsInfo.getId().equals("6225c8276b9e405a9bc61c1bc2d651cc")//#4炉炉渣
                || cProductsInfo.getPdtinfClass().equals("4")//所有其他物资
        ) {
            CTrucks cTrucks2 = new CTrucks();

//                    查询装载点

            if (cAshGateVideoBind != null) {
                if (cAshGateVideoBind.getAshgateSn() != null) {
                    cTrucks2.setTrucksCheckinLoadsiteSn(cAshGateVideoBind.getAshgateSn());
                    QueryWrapper<CTrucks> cTrucksQueryWrapper1 = new QueryWrapper<>();
                    cTrucksQueryWrapper1.ge("TRUCKS_QUEUE_STATUS", 2);
                    cTrucksQueryWrapper1.le("TRUCKS_QUEUE_STATUS", 6);
                    cTrucksQueryWrapper1.eq("TRUCKS_DELETED", 0);
                    cTrucksQueryWrapper1.eq("TRUCKS_CHECKIN_LOADSITE", cAshGateVideoBind.getAshgateName());
                    cTrucksQueryWrapper1.isNotNull("TRUCKS_CHECKIN_TM");
                    cTrucksQueryWrapper1.orderByAsc("TRUCKS_CHECKIN_TM");
                    List<CTrucks> cTrucks1 = iCTrucksService.list(cTrucksQueryWrapper1);
                    // 拿到库容量
//                    Integer vehicleCapacity = cAshGateVideoBind.getVehicleCapacity();

                    // 如果cTrucks1不为空，那就判断，库容量是否够，如果为空就执行这个库
//                    if (cTrucks1.size() < vehicleCapacity) {
//                    叫号
                        cTrucks2.setTrucksQueueStatus(BigDecimal.valueOf(2));
                        cTrucks2.setTrucksCheckinLoadsite(cAshGateVideoBind.getAshgateName());
                        cTrucks2.setTrucksCheckinLoadsiteAdd(getSellMethod(cAshGateVideoBind.getId()));
                        cTrucks2.setSupplyCoId(cAshGateVideoBind.getAshgateSubjectId());
                        cTrucks2.setTrucksCallinTm(callInTM);
                        cTrucks2.setTrucksCheckinLoadsiteSn(cAshGateVideoBind.getAshgateSn());
                        UpdateWrapper<CTrucks> updateWrapper = new UpdateWrapper<>();
                        updateWrapper.eq("ID", cTrucks.getId());
                        iCTrucksService.update(cTrucks2, updateWrapper);
//                    执行微信推送叫号
                        wxInformBu.wxTruckInformFeign(cTrucks);
                        return SzResult.Success();
//                    } else {
//                        return SzResult.error("该装载点车容量已满");
//                    }
                }
            }
        }
        //    直接叫号的品种，这些品种都是由装载点带回装载点编号，然后扣款时要根据装载点去找到是扣公司还是电厂的余额
        if (cProductsInfo.getId().equals("02fadd3e453840a5a19ee50d95a0092a")//转倒加湿灰
                || cProductsInfo.getId().equals("f725ab248ca64e0ba3d3612eb0dab882")//应急加湿灰
                || cProductsInfo.getId().equals("eaf0abcd61c04613aaa0d2387fee8fb6")//应急炉渣
                || cProductsInfo.getId().equals("3cdf25f724764feea4d0d277667a2607")//转倒炉渣
                || cProductsInfo.getId().equals("b9da511ff1dd4969a0e9ce0dbeeb91c9")//加湿灰
        ) {
            CTrucks cTrucks2 = new CTrucks();
            cTrucks2.setTrucksQueueStatus(BigDecimal.valueOf(2));
            cTrucks2.setTrucksCallinTm(callInTM);
            cTrucks2.setTrucksCheckinLoadsite("");
            //                    拿到品种的装载点
            if (cProductsInfo.getPdtinfSpeSitelist() != null && !cProductsInfo.getPdtinfSpeSitelist().equals("")) {
                String[] pdtinfSpeSitelist = cProductsInfo.getPdtinfSpeSitelist().split(",");
                if (pdtinfSpeSitelist.length == 1) {
//                    查询装载点
                    QueryWrapper<CAshGateVideoBind> gateVideoBindWrapper1 = new QueryWrapper<>();
                    gateVideoBindWrapper1.eq("ASHGATE_NAME", pdtinfSpeSitelist[0]);
                    gateVideoBindWrapper1.eq("DATA_FROM", plantId);
                    gateVideoBindWrapper1.eq("ASHGATE_DELETED", 0);
                    CAshGateVideoBind cAshGateVideoBind1 = iCAshGateVideoBindService.getOne(gateVideoBindWrapper1);
                    if (cAshGateVideoBind1 != null) {
                        if (cAshGateVideoBind1.getAshgateSn() != null) {

                            cTrucks2.setTrucksCheckinLoadsiteSn(cAshGateVideoBind1.getAshgateSn());
                            QueryWrapper<CTrucks> cTrucksQueryWrapper1 = new QueryWrapper<>();
                            cTrucksQueryWrapper1.ge("TRUCKS_QUEUE_STATUS", 2);
                            cTrucksQueryWrapper1.le("TRUCKS_QUEUE_STATUS", 6);
                            cTrucksQueryWrapper1.eq("TRUCKS_DELETED", 0);
                            cTrucksQueryWrapper1.eq("TRUCKS_CHECKIN_LOADSITE", cAshGateVideoBind1.getAshgateName());
                            cTrucksQueryWrapper1.isNotNull("TRUCKS_CHECKIN_TM");
                            cTrucksQueryWrapper1.orderByAsc("TRUCKS_CHECKIN_TM");
                            List<CTrucks> cTrucks1 = iCTrucksService.list(cTrucksQueryWrapper1);
                            // 拿到库容量
//                            Integer vehicleCapacity = cAshGateVideoBind1.getVehicleCapacity();
//                            // 如果cTrucks1不为空，那就判断，库容量是否够，如果为空就执行这个库
//                            if (cTrucks1.size() < vehicleCapacity) {
//                    叫号
                                cTrucks2.setTrucksCheckinLoadsite(cAshGateVideoBind1.getAshgateName());
                                cTrucks2.setTrucksCheckinLoadsiteAdd(getSellMethod(cAshGateVideoBind1.getId()));
                                cTrucks2.setTrucksCheckinLoadsiteSn(cAshGateVideoBind1.getAshgateSn());
//                            }
                        }
                    }
                }
            }
//                这里是带回来装载点所以不能在叫号时确定扣款单位
            cTrucks2.setSupplyCoId("");
            cTrucks2.setTrucksQueueVip(1);
//                这里是带回来装载点所以不能在叫号时确定扣款单位
//                cTrucks2.setSupplyCoId(gypsumPlan.getSupplyCoId());
            UpdateWrapper<CTrucks> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("ID", cTrucks.getId());
            iCTrucksService.update(cTrucks2, updateWrapper);
//                微信推送叫号信息
            wxInformBu.wxTruckInformFeign(cTrucks);
            return SzResult.Success();
        }
        QueryWrapper<CTrucks> cTrucksQueryWrapper1 = new QueryWrapper<>();
        cTrucksQueryWrapper1.ge("TRUCKS_QUEUE_STATUS", 2);
        cTrucksQueryWrapper1.le("TRUCKS_QUEUE_STATUS", 6);
        cTrucksQueryWrapper1.ne("TRUCKS_DELETED", 1);
        if (cAshGateVideoBind != null) {
            cTrucksQueryWrapper1.eq("TRUCKS_CHECKIN_LOADSITE", cAshGateVideoBind.getAshgateName());
        }
        cTrucksQueryWrapper1.isNotNull("TRUCKS_CHECKIN_TM");
        cTrucksQueryWrapper1.orderByAsc("TRUCKS_CHECKIN_TM");
        List<CTrucks> cTrucks1 = iCTrucksService.list(cTrucksQueryWrapper1);

        // 拿到库容量
        Integer vehicleCapacity = -1;
        if (cAshGateVideoBind != null) {
            vehicleCapacity = cAshGateVideoBind.getVehicleCapacity();
        }
        // 如果cTrucks1不为空，那就判断，库容量是否够，如果为空就执行这个库
//        if (cTrucks1.size() < vehicleCapacity) {
            CTrucks cTrucks2 = new CTrucks();
              cTrucks2.setTrucksQueueVip(1);
//                    叫号
            cTrucks2.setTrucksQueueStatus(BigDecimal.valueOf(2));
            cTrucks2.setTrucksCheckinLoadsite(cAshGateVideoBind.getAshgateName());
            cTrucks2.setTrucksCheckinLoadsiteAdd(getSellMethod(cAshGateVideoBind.getId()));
            cTrucks2.setSupplyCoId(cAshGateVideoBind.getAshgateSubjectId());
            cTrucks2.setTrucksCallinTm(callInTM);
            cTrucks2.setTrucksCheckinLoadsiteSn(cAshGateVideoBind.getAshgateSn());
            List<CIcCardsInfo> cIcCardsInfos = cIcCardsInfoMapper.selectIcAgentByTruckNo(plantId, cTrucks.getTrucksTruckno());
            String cardauthCardno = "";
            if (cIcCardsInfos.size() == 1) {
                cardauthCardno = cIcCardsInfos.get(0).getCardinfCardno();

            } else if (cIcCardsInfos.size() > 1) {
                return SzResult.Error("该车辆有多个代理卡，请联系管理员");
            } else {
                return SzResult.Error("该车辆没有代理卡，请联系管理员");
            }
            cTrucks2.setTrucksCheckinCardno(cardauthCardno);
            UpdateWrapper<CTrucks> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("ID", cTrucks.getId());
            iCTrucksService.update(cTrucks2, updateWrapper);
//                    执行微信推送叫号
            wxInformBu.wxTruckInformFeign(cTrucks);
            return SzResult.Success();
//        } else {
//            return SzResult.error("该装载点车容量已满");
//        }
    }

    public String getSellMethod(String ashgateId) {

        // 创建查询条件
        QueryWrapper<TAshGateCollocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ASH_GATE_ID1", ashgateId);
        queryWrapper.eq("ENABLE_FLAG", 1);

        // 获取符合条件的TAshGateCollocation列表
        List<TAshGateCollocation> tAshGateCollocations = iTAshGateCollocationService.list(queryWrapper);

        if (tAshGateCollocations.isEmpty()) {
            return "";
        }

        // 返回搭配销售库的名字
        List<String> sellMethod = new ArrayList<>();

        // 确保最多只处理两个元素
        int limit = Math.min(2, tAshGateCollocations.size());

        for (int i = 0; i < limit; i++) {
            TAshGateCollocation collocation = tAshGateCollocations.get(i);
            QueryWrapper<CAshGateVideoBind> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("ID", collocation.getAshGateId2());
            CAshGateVideoBind cAshGateVideoBind = iCAshGateVideoBindService.getOne(queryWrapper1);
            if (cAshGateVideoBind != null) { // 确保查询结果不为空
                sellMethod.add(cAshGateVideoBind.getAshgateName());
            }
        }

        // 将sellMethod列表转换为一个用逗号分隔的字符串
        return String.join(",", sellMethod);
    }
    //       手动叫号这个单位可以选到的装载点
    public SzResult handCallInTruckByAshGate(String trucksCheckinPdt, String trucksCheckinCo) throws Exception {
//        先查出品种的装载点
        QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
        cProductsInfoQueryWrapper.eq("ID", trucksCheckinPdt);
        CProductsInfo cProductsInfo = iCProductsInfoService.getOne(cProductsInfoQueryWrapper);
        //          拿到装载点数据，cProductsInfo里的speloadsitepdtList使用id,id,id
        if (cProductsInfo.getPdtinfSpeSitelist() == null || cProductsInfo.getPdtinfSpeSitelist().equals("")) {
            return SzResult.error("该品种没有绑定装载点");
        }
        String[] speloadsitepdtList = cProductsInfo.getPdtinfSpeSitelist().split(",");
//            speloadsitepdtList转成list
        List<String> list = Arrays.asList(speloadsitepdtList);
//            查询单位绑定了哪些装载点
        QueryWrapper<CCompanys> cCompanysQueryWrapper = new QueryWrapper<>();
        cCompanysQueryWrapper.eq("ID", trucksCheckinCo);
        CCompanys cCompanys = iCCompanysService.getOne(cCompanysQueryWrapper);
        if (cCompanys.getLoadSite() == null || cCompanys.getLoadSite().equals("")) {
            return SzResult.error("该单位没有绑定装载点");
        }
//            查到所有装载点
        String[] loadSite = cCompanys.getLoadSite().split(",");
        //            从装载点表拿到所有装载点
        List<CAshGateVideoBind> intersection = new ArrayList<>();
        for (String s : loadSite) {
            QueryWrapper<CAshGateVideoBind> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("ID", s);
            CAshGateVideoBind cAshGateVideoBind = iCAshGateVideoBindService.getOne(wrapper2);
            intersection.add(cAshGateVideoBind);
        }
//            下面找到对应公司绑定的装载点
// 转换list为Set以提高查找效率
        Set<String> set = new HashSet<>(list);
// 创建一个新列表用于存储匹配的CAshGateVideoBind对象
        List<CAshGateVideoBind> matchedIntersection = new ArrayList<>();
// 遍历intersection，检查ashgateName是否在set中
        for (CAshGateVideoBind bind : intersection) {
            if (set.contains(bind.getAshgateName())) {
                matchedIntersection.add(bind);
            }
        }
//            当一条装载点都匹配不到的话就继续处理其他车
        if (matchedIntersection.size() == 0) {
            return SzResult.error("该单位没有绑定该品种的装载点");
        }
        return SzResult.success(matchedIntersection);
    }
//getTruckCount
public SzResult getAshGateTruckCount(String token, String plantId) throws Exception {

    QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
    queryWrapper.ge("TRUCKS_QUEUE_STATUS", 3);
    queryWrapper.ne("TRUCKS_DELETED", 1);
    queryWrapper.eq("DATA_FROM", plantId);
    queryWrapper.orderByDesc("TRUCKS_QUEUE_STATUS");
    queryWrapper.orderByAsc("TRUCKS_CHECKIN_PDT", "TRUCKS_CHECKIN_LOADSITE", "TRUCKS_CHECKIN_NO");

    // 查询符合条件的数据
    List<CTrucks> list = iCTrucksService.list(queryWrapper);

    // 分组统计：按照 TRUCKS_CHECKIN_LOADSITE 分组，并将空或 null 的记录归为 "其他"
    Map<String, Long> countMap = list.stream()
            .map(item->{
                if (item.getTrucksCheckinPdt() != null) {
                    CProductsInfo productsInfo = iCProductsInfoService.getById(item.getTrucksCheckinPdt());
                    if (productsInfo != null) {
                        item.setTrucksCheckinPdt(productsInfo.getPdtinfPdt());
                    }
                }
                return item;
            })
            .collect(Collectors.groupingBy(
                    truck -> {
                        String product = truck.getTrucksCheckinPdt();
                        return StringUtils.isBlank(product) ? "其他" : product; // 将空或 null 归为 "其他"
                    },
                    Collectors.counting()
            ));
    // 返回结果
    return SzResult.success(countMap);
}
}


